How to extract value by key from json response in PHP

后端 未结 2 671
情话喂你
情话喂你 2021-02-14 15:13

I\'m using getResponse api for getting updated about subscribers. This is what is printing after var_dump($result);

object(stdClass)#2 (1) {
  [\"up         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-14 15:40

    
        // json object.
        $contents = '{"firstName":"John", "lastName":"Doe"}';
    
        // Option 1: through the use of an array.
        $jsonArray = json_decode($contents,true);
    
        $key = "firstName";
    
        $firstName = $jsonArray[$key];
    
    
        // Option 2: through the use of an object.
        $jsonObj = json_decode($contents);
    
        $firstName = $jsonObj->$key;
    
    

提交回复
热议问题