请帮助,因为我花了两天时间......
我有一个JSON对象。如下所示。
[{"attr":{"id":"node_8","rel":"folder"},"data":"_demo","state":"closed"},{"attr": {"id":"node_13","rel":"folder"},"data":"demo3","state":""}][{"attr":{"id":"node_8","rel":"folder"},"data":"_demo","state":"closed"},{"attr":{"id":"node_13","rel":"folder"},"data":"demo3","state":""}]string(140) "[{"attr":{"id":"node_8","rel":"folder"},"data":"_demo","state":"closed"},{"attr":{"id":"node_13","rel":"folder"},"data":"demo3","state":""}]"
使用Json解码我得到以下输出..
Array ( [0] => Array ( [attr] => Array ( [id] => node_8 [rel] => folder ) [data] => _demo [state] => closed ) [1] => Array ( [attr] => Array ( [id] => node_13 [rel] => folder ) [data] => demo3 [state] => ) ) aArray
如何迭代并以列表形式访问每个值,以便添加div类和id。例如[id] => node_8。如何访问该值并将其转换为div id =“node_8”或[rel] =>文件夹,并转换为div class =“folder”。例如,我希望这是有道理的 例如
foreach ($data as $key => $value){
if(is_array($value)) {
{
echo $value . "<br />";
}
}
}
产生id = node_8 rel =文件夹数据= _demo state = closed id = node_13 rel =文件夹数据= demo3 state =
如果有人遇到多维数组问题,并从JSON解码,将div等添加到数组中,我已经添加了以下答案。由Shayan Husaini提供。 其中$ string等于数组。
$json_a=json_decode($string,true);
foreach ($json_a as $value) {
回声'答案 0 :(得分:3)
您的数组是多维数组,因此您需要为子数组定义键以获取值。我希望这会对你有所帮助:
foreach ($data as $value){
echo 'id: '.$value['attr']['id'];
echo 'rel: '.$value['attr']['rel'];
echo 'demo: '.$value['demo'];
echo 'state: '.$value['state'];
}