PHP Json循环提取数据

时间:2018-09-26 10:52:33

标签: php json laravel

我有json变量。

当我执行var_dump时,结果将如下所示:

array(2) { ["refQualifier"]=> string(1) "S" ["refNumber"]=> int(1) } 
array(2) { ["refQualifier"]=> string(1) "S" ["refNumber"]=> int(2) } 
array(2) { ["refQualifier"]=> string(1) "B" ["refNumber"]=> int(2) } 

当我做print_r时会像这样:

Array ( [refQualifier] => S [refNumber] => 1 ) 1
Array ( [refQualifier] => S [refNumber] => 2 ) 1
Array ( [refQualifier] => B [refNumber] => 2 ) 1

我的问题是如何循环播放。我需要提取refNumber。而且我正在使用PHP和Laravel。

3 个答案:

答案 0 :(得分:0)

您可以直接获取为数组

首先json_decode您的json数据,然后传入foreach循环

foreach($recommendation->response as $recomend)
  {
     foreach($recomend as $rec)
     {
        foreach($rec->segmentFlightRef as $segment)
        {
            foreach($segment as $userData)
            {
                foreach ($userData as $data)
                {
                    print_r($data->refQualifier);
                }

            }

        }
     }

  }

答案 1 :(得分:0)

foreach ($json as $item) { 
   echo $item['refNumber'];
}

也可以在以下视图中使用Blade语法完成:

@foreach ($json as $item)

   {{ $item['refNumber'] }}

@endforeach

答案 2 :(得分:0)

似乎您有这样的关联数组

$data = [
    ['refQualified' => "S", 'refNumber' => 1],
    ['refQualified' => "S", 'refNumber' => 2],
    ['refQualified' => "B", 'refNumber' => 2]
];

并访问视图中的每个refNumber都可以这样做

@foreach($datas as $data)
   {{ $item['refNumber'] }}
@endforeach