第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 PHP 中實(shí)現(xiàn)接受字符串參數(shù)并返回 JSON 格式的方法

如何在 PHP 中實(shí)現(xiàn)接受字符串參數(shù)并返回 JSON 格式的方法

PHP
慕村225694 2022-01-02 19:56:54
以下是定義水果顏色的數(shù)據(jù)結(jié)構(gòu)示例:array(  "red" => array("apple, strawberry"),  "yellow" => array("lemon", "ripe mango"))實(shí)現(xiàn)函數(shù) getFruits 方法,該方法接受顏色作為字符串參數(shù)并以 JSON 格式返回該顏色的所有水果(請(qǐng)參見下面的示例)。例如,調(diào)用 $fruitcolors->getFruits("red"); 應(yīng)該返回:{“顏色”:“紅色”,“水果”:[“蘋果”,“草莓”]}如果調(diào)用沒有該 $fruitcolors- getFruits("violet"); 的水果;顏色,它應(yīng)該返回:{“顏色”:“紫羅蘭”,“水果”:[] }<?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貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

試試這個(gè):


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));

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-01-02
?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2022-01-02
?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

這可能是您正在尋找的內(nèi)容:


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


查看完整回答
反對(duì) 回復(fù) 2022-01-02
  • 3 回答
  • 0 關(guān)注
  • 253 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)