我有以下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。
答案 0 :(得分: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/>';
}
}
答案 1 :(得分: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/>";
}
}
?>
我建议你在下次遇到麻烦时使用函数var_dump($ var),它会帮助你弄清楚出了什么问题。
答案 2 :(得分:0)
如果您执行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