Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

HI I need to parse a multi dimensional array

$myArray = array(
    array('id' => 6),
    array(
        'id' => 3,
        'children' => array(
            'id' => 5,
            'children' => array(
                'id' => 7,
                'children' => array(
                    array('id' => 4), 
                    array('id' => 1)
                ),
                array('id' => 8)
            )
        )
    ),
    array('id' => 2)
);

Here's the output I need as a string or array...

6
3
3,5
3,5,7
3,5,7,4
3,5,7,1
3,5,8
2
share|improve this question
2  
What is your question? –  dmmd Aug 4 '12 at 15:51
 
Implode and a simple loop should do. –  Mahn Aug 4 '12 at 15:52
 
this is not a site to ask such give me the code questions, first you have to try. –  code-jaff Aug 4 '12 at 15:58
 
Are you sure, your data structure is correct this way? I just reformatted the array you gave us and as you see, children sometimes onsists of one, sometimes of two objects. –  Lars Knickrehm Aug 4 '12 at 16:01
 
Additionally you can see, that array('id' => 8) does have none (a numeric) key... –  Lars Knickrehm Aug 4 '12 at 16:02
show 1 more comment

closed as not a real question by Kev Aug 4 '12 at 16:03

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

You need to create a recursive loop:

$children = array();

function getChilren($myArray, $children){

     foreach($myArray as $value){
          if(is_array($value)){
              $cLen = count($children);
              $children[] = $children[$cLen-1];
              getChildren($value, $children[$cLen]);
          }
          else {
              $children[] = $value;
          }
      }
 }

This may be flawed, and need more to work, but I think its at least a start.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.