【问题标题】:PHP JSON get key and valuePHP JSON 获取键和值
【发布时间】:2018-01-04 20:42:59
【问题描述】:

我有以下 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 元素...这是存储在 mysql 数据库中的 JSON 格式,并被读取到需要解析上述输出中的 JSON 的 php 脚本中,所以我可以使用输出创建 ul 和 li 元素。

我尝试执行 foreach 但结果为空?我知道当我得到价值时,我需要使用explode(', ', $value) 进行爆炸字符串,但我无法在 PHP 中根据需要读取 $value 和 $key。

【问题讨论】:

  • 默认情况下,json_decode 将元素输出为对象,如果您想要关联数组而不是循环,请使用 json_decode($string, true)
  • 你的$val 是一个数组(看看json的结构)。您还需要像 foreach ($val as $item){ // your code} 这样的内部循环

标签: php json


【解决方案1】:

这解决了您的问题,您必须将 $json 转换为数组,因为它被视为 stdClass 对象;)

<?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/>";
    }
}
?>

Try It Online!

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

【讨论】:

    【解决方案2】:
    $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/>';
      }
    }
    

    【讨论】:

      【解决方案3】:

      如果您执行 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”是一个必须循环遍历的数组。

      所以你需要遍历每个键,然后遍历每个 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
      

      【讨论】:

      • 感谢工作......但我更喜欢简单的短代码......但这也有效......感谢编写代码......并帮助我
      猜你喜欢
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2011-10-27
      • 2014-07-12
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多