-1

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 '';

6
  • i dont get the problem. why not use foreach? Commented Mar 5, 2012 at 22:42
  • Have you even bothered to read php's tutorial or the documentation about arrays ? Commented Mar 5, 2012 at 22:42
  • I would love to know what you have tried over the past two days Commented Mar 5, 2012 at 22:44
  • I have gone through every example for the for each array, I dont know how to append to each element in the array. I can extract the information, but I cant append anything to each individual element of the array. 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 = Commented Mar 5, 2012 at 22:56
  • Okay, good. You've echoed out the values of the array. What's your issue? Commented Mar 5, 2012 at 23:07

1 Answer 1

3

Your array is multidimensional array so you need to define the keys as well for child arrays to get the values. I hope this would help you:

foreach ($data as $value){
       echo 'id: '.$value['attr']['id'];
       echo 'rel: '.$value['attr']['rel'];
       echo 'demo: '.$value['demo'];
       echo 'state: '.$value['state'];
  } 
1
  • 1
    THANK YOU SO MUCH Shayan. My computer was just to go through the window lol. echo 'id: '.$value['attr']['id']; was what I was looking for. I could not get that to display. You are an absolute star. Thank you for making the time to comment. I appreciate it so much as its 3 days I spent on this, and its something so simple, that you can spot straight away. Many many thanks again. Commented Mar 6, 2012 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.