我有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。
答案 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