PHP JSON获取键和值

6

我有以下JSON格式:

{
    "S01": ["001.cbf", "002.cbf", "003.cbf", "004.cbf", "005.cbf", "006.cbf", "007.cbf", "008.cbf", "009.cbf"],
    "S02": ["001.sda", "002.sda", "003.sda"],
    "S03": ["001.klm", "002.klm"]
}

我尝试使用这段代码:
$json = json_decode('{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}');

foreach($json as $key => $val) {
     if ($key) { echo 'KEY IS: '.$key; };
     if ($val) { echo 'VALUE IS: '.$value; };
     echo '<br>';
}

但是我得到了空结果...我需要获得如下输出:
KEY IS: S01
VALUE IS: 001.cbf
VALUE IS: 002.cbf
VALUE IS: 003.cbf
VALUE IS: 004.cbf
VALUE IS: 005.cbf
VALUE IS: 006.cbf
VALUE IS: 007.cbf
VALUE IS: 008.cbf
VALUE IS: 009.cbf

KEY IS: S02
VALUE IS: 001.sda
VALUE IS: 002.sda
VALUE IS: 003.sda

KEY IS: S03
VALUE IS: 001.klm
VALUE IS: 002.klm

我需要这个,以便使用值和键名生成ul和li元素... 这是以JSON格式存储在mysql数据库中的内容,并且被读取到需要解析该JSON的PHP脚本中,以便我可以使用输出创建ul和li元素。
我尝试使用foreach,但我得到了空结果? 我知道当我得到所需的值时,我需要使用explode(',',$ value)来分解字符串,但我无法按照PHP所需的方式读取$value和$key。

1
默认情况下,json_decode 输出元素为对象,如果您想要一个关联数组来进行循环,请使用 json_decode($string, true) - Kaddath
你的 $val 是一个数组(查看 JSON 的结构)。你还需要一个内部循环,像这样 foreach ($val as $item){ // 你的代码} - YvesLeBorg
3个回答

8
这解决了你的问题,因为它被认为是stdClass对象,所以你必须将$json转换为数组 ;)
<?php 
$json = json_decode('{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}');
foreach($json as $key => $val) {
    echo "KEY IS: $key<br/>";
    foreach(((array)$json)[$key] as $val2) {
        echo "VALUE IS: $val2<br/>";
    }
}
?>

在线尝试!

下次遇到问题时,我建议您使用函数var_dump($var),它将帮助您找出问题所在。


5
$json = '{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}';

$array = json_decode($json, true);

foreach($array as $key => $val) {
  echo 'KEY IS:'.$key.'<br/>';
  foreach($val as $_key => $_val) {
   echo 'VALUE IS: '.$_val.'<br/>';
  }
}

4
如果您执行 var_dump($json) 来观察结果,您会看到它的样子是这样的...
$json = json_decode('{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}');

var_dump($json);


object(stdClass)[1]
  public 'S01' => 
    array (size=9)
      0 => string '001.cbf' (length=7)
      1 => string '002.cbf' (length=7)
      2 => string '003.cbf' (length=7)
      3 => string '004.cbf' (length=7)
      4 => string '005.cbf' (length=7)
      5 => string '006.cbf' (length=7)
      6 => string '007.cbf' (length=7)
      7 => string '008.cbf' (length=7)
      8 => string '009.cbf' (length=7)
  public 'S02' => 
    array (size=3)
      0 => string '001.sda' (length=7)
      1 => string '002.sda' (length=7)
      2 => string '003.sda' (length=7)
  public 'S03' => 
    array (size=2)
      0 => string '001.klm' (length=7)
      1 => string '002.klm' (length=7)

因此,实际上您拥有一个数组的数组。

因此,对于每个“键”,相关联的“值”是一个数组,您需要循环遍历该数组。

所以您需要遍历每个键,然后遍历每个val数组。

foreach ($json as $key => $val) {
    echo 'KEY IS: ' . $key;
    echo '<br>';
    foreach ($val as $value) {
        echo 'VALUE IS: ' . $value;
        echo '<br>';
    }
    echo '<br>';
}

生成的输出是...
KEY IS: S01
VALUE IS: 001.cbf
VALUE IS: 002.cbf
VALUE IS: 003.cbf
VALUE IS: 004.cbf
VALUE IS: 005.cbf
VALUE IS: 006.cbf
VALUE IS: 007.cbf
VALUE IS: 008.cbf
VALUE IS: 009.cbf

KEY IS: S02
VALUE IS: 001.sda
VALUE IS: 002.sda
VALUE IS: 003.sda

KEY IS: S03
VALUE IS: 001.klm
VALUE IS: 002.klmlm
VALUE IS: 002.klm

谢谢,这个代码运行得很好...但我更偏向于简单短小的代码...不过这个也可以用...感谢您编写代码并帮助我。 - Igor Petev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接