如何在 PHP 中实现接受字符串参数并返回 JSON 格式的方法

以下是定义水果颜色的数据结构示例:


array(

  "red" => array("apple, strawberry"),

  "yellow" => array("lemon", "ripe mango")

)

实现函数 getFruits 方法,该方法接受颜色作为字符串参数并以 JSON 格式返回该颜色的所有水果(请参见下面的示例)。


例如,调用 $fruitcolors->getFruits("red"); 应该返回:


{“颜色”:“红色”,“水果”:[“苹果”,“草莓”]}


如果调用没有该 $fruitcolors- getFruits("violet"); 的水果;颜色,它应该返回:


{“颜色”:“紫罗兰”,“水果”:[] }


<?php

class FruitColor

{

  private $fruitcolor;


  function FruitColor($fruitcolor)

  {

    $this->fruitcolor = $fruitcolor;

  }


  public function getFruits($color)

  {

    // @todo: implement here

    return NULL;

  }

}


$fruitcolor = new FruitColor(array(

    "red" => array("apple", "strawberry"),

    "yellow" => array("lemon", "ripe mango")

));


echo $fruitcolor->getFruits("red");

echo "\n";

echo $fruitcolor->getFruits("violet");


慕村225694
浏览 205回答 3
3回答

鸿蒙传说

试试这个:class FruitColor {&nbsp; &nbsp; private $fruitcolor;&nbsp; &nbsp; function __construct($fruitcolor)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->fruitcolor = $fruitcolor;&nbsp; &nbsp; }&nbsp; &nbsp; public function getFruits($color)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $fruits = array();&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->fruitcolor[$color])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fruits = $this->fruitcolor[$color];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return json_encode(array("color" => $color, "fruits" => $fruits));&nbsp; &nbsp; }}

慕慕森

class FruitColor{&nbsp; &nbsp; private $fruitcolor;&nbsp; &nbsp; public function __construct( $fruitcolor ){&nbsp; &nbsp; &nbsp; &nbsp; $this->fruitcolor = $fruitcolor;&nbsp; &nbsp; }&nbsp; &nbsp; public function getFruits( $color ){&nbsp; &nbsp; &nbsp; &nbsp; if( array_key_exists( $color, $this->fruitcolor ) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return json_encode( (object)array('color'=>$color, 'fruits' => $this->fruitcolor[ $color ]) );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return json_encode( (object)array('color'=>$color, 'fruits' => array() ) );&nbsp; &nbsp; }}$arr=array(&nbsp; &nbsp; "red"&nbsp; &nbsp; &nbsp; &nbsp;=> array("apple", "strawberry"),&nbsp; &nbsp; "yellow"&nbsp; &nbsp; => array("lemon", "ripe mango"));$obj=new FruitColor( $arr );$red=$obj->getFruits( 'red' );$yellow=$obj->getFruits( 'yellow' );$violet=$obj->getFruits( 'violet' );printf('<pre>%s</pre>',print_r( $red,1));printf('<pre>%s</pre>',print_r( $yellow,1));printf('<pre>%s</pre>',print_r( $violet,1));将输出:{"color":"red","fruits":["apple","strawberry"]}{"color":"yellow","fruits":["lemon","ripe mango"]}{"color":"violet","fruits":[]}

皈依舞

这可能是您正在寻找的内容:<?phpclass FruitColor {&nbsp; &nbsp; private $data;&nbsp; &nbsp; function FruitColor($fruitcolors) {&nbsp; &nbsp; &nbsp; &nbsp; $this->data = $fruitcolors;&nbsp; &nbsp; }&nbsp; &nbsp; public function getFruits($color) {&nbsp; &nbsp; &nbsp; &nbsp; $fruits = [];&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->data[$color]) && is_array($this->data[$color])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fruits = $this->data[$color];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return json_encode(array("color" => $color, "fruits" => $fruits));&nbsp; &nbsp; }}$fruitcolor = new FruitColor([&nbsp; &nbsp; "red" => ["apple", "strawberry"],&nbsp; &nbsp; "yellow" => ["lemon", "ripe mango"]]);var_dump($fruitcolor->getFruits("red"));var_dump($fruitcolor->getFruits("yellow"));var_dump($fruitcolor->getFruits("violet"));输出显然是:string(47) "{"color":"red","fruits":["apple","strawberry"]}"string(50) "{"color":"yellow","fruits":["lemon","ripe mango"]}"string(30) "{"color":"violet","fruits":[]}"
打开App,查看更多内容
随时随地看视频慕课网APP