亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 PHP 中實現接受字符串參數并返回 JSON 格式的方法

如何在 PHP 中實現接受字符串參數并返回 JSON 格式的方法

PHP
慕村225694 2022-01-02 19:56:54
以下是定義水果顏色的數據結構示例:array(  "red" => array("apple, strawberry"),  "yellow" => array("lemon", "ripe mango"))實現函數 getFruits 方法,該方法接受顏色作為字符串參數并以 JSON 格式返回該顏色的所有水果(請參見下面的示例)。例如,調用 $fruitcolors->getFruits("red"); 應該返回:{“顏色”:“紅色”,“水果”:[“蘋果”,“草莓”]}如果調用沒有該 $fruitcolors- getFruits("violet"); 的水果;顏色,它應該返回:{“顏色”:“紫羅蘭”,“水果”:[] }<?phpclass 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");
查看完整描述

3 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

試試這個:


class FruitColor {


    private $fruitcolor;


    function __construct($fruitcolor)

    {

        $this->fruitcolor = $fruitcolor;

    }

    public function getFruits($color)

    {

        $fruits = array();

        if (isset($this->fruitcolor[$color])) {

            $fruits = $this->fruitcolor[$color];

        }

        return json_encode(array("color" => $color, "fruits" => $fruits));

    }

}


查看完整回答
反對 回復 2022-01-02
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

class FruitColor{


    private $fruitcolor;


    public function __construct( $fruitcolor ){

        $this->fruitcolor = $fruitcolor;

    }


    public function getFruits( $color ){

        if( array_key_exists( $color, $this->fruitcolor ) ){

            return json_encode( (object)array('color'=>$color, 'fruits' => $this->fruitcolor[ $color ]) );

        }

        return json_encode( (object)array('color'=>$color, 'fruits' => array() ) );

    }

}





$arr=array(

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

    "yellow"    => 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":[]}


查看完整回答
反對 回復 2022-01-02
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

這可能是您正在尋找的內容:


<?php


class FruitColor {

    private $data;


    function FruitColor($fruitcolors) {

        $this->data = $fruitcolors;

    }


    public function getFruits($color) {

        $fruits = [];

        if (isset($this->data[$color]) && is_array($this->data[$color])) {

            $fruits = $this->data[$color];

        }

        return json_encode(array("color" => $color, "fruits" => $fruits));

    }

}


$fruitcolor = new FruitColor([

    "red" => ["apple", "strawberry"],

    "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":[]}"


查看完整回答
反對 回復 2022-01-02
  • 3 回答
  • 0 關注
  • 222 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號