我正在尝试用php读取一个特殊的json文件。这是文件:
[
[
{
"Programpart": "Ball",
"version": "0.5.1",
"date": "2014-06-01" ,
"file_name":"MyProgram-0.5.1",
"name":"MyProgram",
"author":"Peter",
}
]
,
[
{
"Programpart": "Court",
"version": "0.5.2",
"date": "2014-06-02" ,
"file_name":"MyProgram-0.5.2",
"name":"MyProgram",
"author":"Peter",
}
]
]
我的问题是,对象和数组没有名称。我用一个简单的标准json文件做到了,但我不能用这个做。我需要获取“Programpart”的所有值以及“version”的相应值。 我怎么能这样做?
我知道有类似的问题,但我已经尝试了很多东西,但仍然无法弄明白。感谢。
答案 0 :(得分:0)
$string = '[
[
{
"Programpart": "Ball",
"version": "0.5.1",
"date": "2014-06-01" ,
"file_name":"MyProgram-0.5.1",
"name":"MyProgram",
"author":"Peter"
}
]
,
[
{
"Programpart": "Court",
"version": "0.5.2",
"date": "2014-06-02" ,
"file_name":"MyProgram-0.5.2",
"name":"MyProgram",
"author":"Peter"
}
]
]';
$json = json_decode($string, true);
foreach ($json as $outer_array) {
foreach ($outer_array as $inner_array) {
echo $inner_array['Programpart'].' = '.$inner_array['version'];
}
}
试试 - 我必须删除作者行末尾的逗号。