PHP:环路上的JSON?
问题描述:
我有以下JSON:PHP:环路上的JSON?
{ “开关”:[ “选择1”, “选择2”, “choice3”], “check_mk”:[ “选择1”, “选择2”, “choice3”] “测试组”:[ “选择1”, “选择2”, “choice3”], “打印机”:[ “选择1”, “选择2”, “choice3”], “CAD”:[ “选择1”, “选择2” “choice3”]}
我怎么循环的每个对象在使用PHP?
我的想法是以下几点:
<?php
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
foreach($decode as $key => $value) {
echo $key . $value;
}
?>
不行的.....所以
echo $decode[1];
和
echo $decode[1][1];
不工作..
答
你需要第二个参数添加到json_decode()
此参数返回关联数组代替现有的对象(如果存在)。
$decode = json_decode($jsonfile, TRUE);
这将转换您的JSON解码后的数据为关联数组。
答
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
$decode
相当于现在:
$decode = new stdClass();
$decode->Switches = array();
$decode->Switches[] = "Auswahl1";
$decode->Switches[] = "Auswahl2";
$decode->Switches[] = "Auswahl3";
$decode->Check_MK = array();
...
问题是什么? –