仅Foreach循环回波第一次出现(仅第一个元素)

时间:2019-06-16 08:52:41

标签: php json loops foreach

我一直在尝试选择foreach循环中的第一个匹配项。这是我正在使用的代码:

<?php
$data = file_get_contents('data.json');
$data = json_decode($data,true);
foreach($data['screenshots'] as $values){
    echo $values[0];
}
?>

它仅回显“ hhhhhhhhhhhhhhhhhhhhhhh”。

这是JSON数据:pastebin.com/WqyJBAbg

1 个答案:

答案 0 :(得分:0)

这应该有所帮助。

$data = file_get_contents('data.json');
$data = json_decode($data,true);

foreach($data['screenshots'] as $index => $value) {
  if ($index == 0) {
       echo $data['screenshots'][$index];
  }
}