这是我的.json文件
阵 ( [0] =>排列 ( [id] => 1 [name] =>冰雕 [price] => 12.5 [tags] =>排列 ( [0] =>冷 [1] =>冰 )
[dimensions] => Array
(
[length] => 7
[width] => 12
[height] => 9.5
)
[warehouselocation] => Array
(
[latitude] => -78.75
[longitude] => 20.4
)
)
[1] => Array
(
[id] => 2
[name] => A blue mouse
[price] => 25.5
[tags] => Array
(
[0] => cold
[1] => ice
)
[dimensions] => Array
(
[length] => 3.1
[width] => 1
[height] => 1
)
[warehouselocation] => Array
(
[latitude] => 54.4
[longitude] => -32.7
)
)
[2] => Array
(
[id] => 2
[name] => A blue mouse
[price] => 25.5
[dimensions] => Array
(
[length] => 3.1
[width] => 1
[height] => 1
)
[warehouselocation] => Array
(
[latitude] => 54.4
[longitude] => -32.7
)
)
[3] => Array
(
[id] => 3
[name] => A Chainsmoker
[price] => 99.5
[tags] => Array
(
[0] => king
[1] => wils
)
[dimensions] => Array
(
[length] => 7.1
[width] => 25
[height] => 7
)
[warehouselocation] => Array
(
[latitude] => 54.4
[longitude] => -32.7
)
)
) 这里我想显示的数据只有价格大于15.0。 如何在 Laravel
中使用foreach进行显示`$产品列表的file_get_contents =(' C:\ XAMPP \ htdocs中\ Laravel \资源\数据\ ProductList.json&#39);
$数据= json_decode($产品列表,TRUE);
//的print_r($数据);
foreach($data as $key => $value) {
if ($key == 'price' && $value > 15)
echo $value;
}
print_r($value);
`
答案 0 :(得分:0)
如果您的数组名称是$ data,那么您可以像这样使用foreach
@foreach($data as $key => $value)
@if ($value['price'] > 15.0)
echo $value['price'];
@endif
@endforeach
在上面的代码中,我只是打印价格,但您可以在" echo $ value ['价格'];"
的地方编写代码我希望它对你有所帮助
答案 1 :(得分:0)
您可以使用array_filter()函数。
$result = array_filter($input, function($value) {
return $value['price'] > 15;
});
在上面的示例中,array_filter将循环遍历$ input数组,并且仅当price高于15时才将值添加到$ result数组。