0

I am pulling through json arrays into Jquery - no problem, just how do you parse a multi-dimensional array where you might have "children" arrays.

PHP to "create" the json pulling from a database

   $STH = $DBH->query("SELECT FID FROM flist WHERE ParentID='0'" );
$STH->setFetchMode(PDO::FETCH_OBJ);
while( $row = $STH->fetch()) : 
 $farray[$row->FID]['FID']  = $row->FID;
  $RTH = $DBH->query("SELECT FID FROM flist WHERE ParentID= ".$row->FID." " );
  $RTH->setFetchMode(PDO::FETCH_OBJ);
  $fquerycount = $RTH->rowCount();
  if($fquerycount!=0): 
   while( $res = $RTH->fetch()) : 
    $farray[$row->FID]['FCHILD'][$res->FID] = $res->FID;
   endwhile;
  endif;
endwhile;

encode it thus:

echo  $jc . '(' . json_encode($forum)  . ')';

Note: $jc is a jsoncallback.

Now how do you "read" it here:

   $.getJSON("http://127.0.1/jtest3.php?start=0&jsoncallback=?", function(data){ 
$.each(data, function(i,item){         
$('#testit').append('<li'+item.FID+'<ul><li>'+item.FCHILD+'</li><ul></li>');
}); 
});

It seems to me - OK I'm sure it must need - another .each() loop for the child array? The code works OK for all aspects other than displaying the child. If I remove the [$res->FID] key from the child array then that is OK but obviously I only get the last DB entry, The problem is that the child array is more than 1 entry. You will have 1 parent (the first query that produces $farray[$row->FID]['FID'] = $row->FID;) then children the second query i.e. $farray[$row->FID]['FCHILD'][$res->FID] = $res->FID; Now there could be 2, 10, 20 etc. in this array. How do you run that second each loop? Help much appreciated. Thanks

1 Answer 1

0

Check this thread Writing Multidemisional Array jQuery and to be more specific this comment

"there are no multidimensional arrays in javascript, but you can have an array whose elements are arrays"

So yes, you have to check each array element if it is an array also and cycle through it.

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.