Please help as I have spent two days on this...
I have a JSON object. as shown below.
[{"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":""}]"
Using Json decode I get the following output..
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
How can I iterate through and access each value as a list so I can add div classes, and id's. For instance [id] => node_8. How can I access that value and convert it to div id = "node_8", or [rel] => folder, and convert to div class = "folder". For example I hope that makes sense
for example
foreach ($data as $key => $value){
if(is_array($value)) {
{
echo $value . "<br />";
}
}
}
which produces id = node_8 rel = folder data = _demo state = closed id = node_13 rel = folder data = demo3 state =
I have added the answer below if anyone has problems with multi dimension arrays, and decoding from JSON, adding divs etc to the array. Kindly provided by Shayan Husaini. Where $string is equal to array.
$json_a=json_decode($string,true);
foreach ($json_a as $value) {
echo '
';
echo 'id: '.$value['attr']['id'];
echo '
';
echo 'rel: '.$value['attr']['rel'];
echo '
';
echo 'name: '.$value['data'];
echo '
';
echo '' .$value['state'];
echo '';