【问题标题】:How to print the values from an array return by a json response?如何打印由 json 响应返回的数组中的值?
【发布时间】:2017-02-27 14:09:20
【问题描述】:

我正在尝试将一个 json 数组打印到它的值中,我在 json 解码后有以下数组,我需要循环打印它,因为它有很多项目。 例如,我必须打印值 firstName, lastName ,我将如何打印它。

stdClass Object ( [content] => Array ( [0] => stdClass Object ( [id] => 5 [firstName] => Ali [lastName] => S [profilePhotoUrl] => https://prn-spe-images.s3-ap-southeast-1.amazonaws.com/user-profiles/5.jpg [handle] => aliya ) [1] => stdClass Object ( [id] => 69 [handle] => hhtc ) ) [last] => 1 [totalPages] => 1 [totalElements] => 2 [numberOfElements] => 2 [first] => 1 [sort] => [size] => 10 [number] => 0 )

我已经完成的示例代码:

$arrayl = gettviewers($post->id,10);
foreach($arrayl as $valuel) {
  print $value1->content->firstName;
}

但它没有打印任何值。

### 以下答案的完整工作代码:###

帮助文件:

 ####### --- Get live viwers list for each broadcast --- ########
if ( ! function_exists('gettviewers'))
{ 
      function gettviewers($broId,$size){
      $CI =& get_instance();
      $url = API_SERVER.'broadcasts/public/'.$broId.'/top-viewers?size='.$size; 
      $json = json_decode(file_get_contents($url), true);
      return $json;
      }   
}

在视图中:

$arrayl = gettviewers($post->id, WI_LIVEUSERSIZE);
              foreach($arrayl as $value1) 
                { 
                 print $value1[0][firstName];
                 }

【问题讨论】:

    标签: php arrays json multidimensional-array


    【解决方案1】:

    Foreach “获取”对象或数组的每个元素。所以当你做 foreach 你的 $value1 不是

    stdClass Object ( [content] => Array ( [0] => stdClass Object ( [id] => 5 [firstName] => Ali [lastName] => S [profilePhotoUrl] => https://prn-spe-images.s3-ap-southeast-1.amazonaws.com/user-profiles/5.jpg [handle] => aliya ) [1] => stdClass Object ( [id] => 69 [handle] => hhtc ) ) [last] => 1 [totalPages] => 1 [totalElements] => 2 [numberOfElements] => 2 [first] => 1 [sort] => [size] => 10 [number] => 0 )
    

    但是

    [content] => Array ( [0] => stdClass Object ( [id] => 5 [firstName] => Ali [lastName] => S [profilePhotoUrl] => https://prn-spe-images.s3-ap-southeast-1.amazonaws.com/user-profiles/5.jpg [handle] => aliya ) [1] => stdClass Object ( [id] => 69 [handle] => hhtc ) ) [last] => 1 [totalPages] => 1 [totalElements] => 2 [numberOfElements] => 2 [first] => 1 [sort] => [size] => 10 [number] => 0
    

    所以你的$value1 已经是[content]。另外你应该注意到[content] 是一个数组,所以你必须这样做

    $arrayl = gettviewers($post->id,10);
    foreach($arrayl as $valuel) {
      print $value1[0]->firstName;
    }
    

    或者在第一个循环中使用第二个 foreach 循环。

    【讨论】:

    • 谢谢@Wolen 我这样打印:print $value1[0][firstName];
    【解决方案2】:

    您可以使用json_decode 实现此目的

    json_decode 将有效的 JSON 转换为 stdClass-Object。

    试试这个:

    $arrayl = json_decode(gettviewers($post->id, 10));
    
    foreach($arrayl as $value1) 
    {
      print $value1->content->firstName;
    }
    

    【讨论】:

    • 谢谢 Vural。我已经完成了 json 解码。谢谢你的回答。
    【解决方案3】:

    试试这个

    $arrayl = gettviewers($post->id,10);
    
    
        if (is_object($arrayl)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $arrayl = get_object_vars($arrayl);
        }
    
        if (is_array($arrayl)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            $arrayl = array_map(__FUNCTION__, $arrayl);
        }
        else {
            // Return array
            return $arrayl;
        }
    
    var_dump($arrayl) or print_r($arrayl);
    

     foreach($arrayl as $key=>$val){
         echo $val->content[$key]
        }
    

    【讨论】:

    • 你为什么将它设置为 true?他想要一个 stdObject 而不是一个数组
    【解决方案4】:

    要打印 json_encoded 数组,请执行此操作

    $array=json_decode($data,true); //data is your json_encoded data
    
    foreach($array as $key=>$value)
    {
    echo $key . ' ' . $value . PHP_EOL;
    }
    

    或者干脆做

    print_r($array);
    

    解码后。

    【讨论】:

    • 感谢迪米的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多