0

I have a multidimensional array that when i use print_r looks like this:

Array ( 

[car] => Array (   
           [72] => Camry 
           [62] => Lexus 
           [55] => honda 
           [44] => toyota 
                )

[Job] => Array (   
           [70] => Bookstore
           [73] => Cafe 
               ) 

[City] => Array (  
           [68] => Wisconsin
           [63] => Chicago 
           [47] => New York City 
           [46] => Los Angeles 
                ) 
 ) 

This is a Parent/Child Multidimensional array. There can be any number of parents, and any number of children.

How do I echo out a multidimensional array so that it looks like this

Car
    Camry 
    Lexus 
    honda 
    toyota

Job
    Bookstore
    Cafe

City
    Wisconsin
    Chicago
    New York City
    Los Angeles

My attempt at printing a multidimensional array out:

function RecursiveWrite($array) {
                      foreach ($array as $vals) {
                          echo $vals['parent'] . "\n";
                          RecursiveWrite($vals['child']);
                      }
                  }     
RecursiveWrite($categories); // $categories is the name of the array 

This doesn't print out any output when I run the code. Any suggestions?

4
  • where exactly want to echo it?Is it a html table or some page?If you are fetching it from database,you will need either a foreach or a while loop to print them one by one. Commented Dec 1, 2012 at 5:40
  • This is coming from a database, and I'm echoing this out in a div, not a table. Commented Dec 1, 2012 at 5:42
  • you have the right idea, you just have to add conditionals inside the foreach loop to test whether you need to recursively call the function again Commented Dec 1, 2012 at 5:45
  • @Jeffrey's answer should work.When printing multidimensional arrays you need to give the key pointing the value in the foreach loop! Commented Dec 1, 2012 at 5:57

3 Answers 3

1

Considering that the tab and return symbols depends on whatever you want an HTML or FILE output here it is the code:

$tab = "\t"; // tab
$return = "\r"; // return

foreach ($array as $key => $a) {
    echo $key.$return;
    foreach ($a as $value) {
        echo $tab.$value.$return;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Create a recursive function:

Semi-psuedo:

function dumpArr (arr){
  foreach element in arr {
    if element is array/object{
      dumpArr(element)
    }else{
      echo element
    }
  }
}

then, you can use CSS to adjust the padding, margin, etc

Comments

0

the below code shows as you want:

 <?php
$arrayToPrint = Array (
        'car' => Array (   
        '72' => 'Camry', 
        '62' => 'Lexus', 
        '55' => 'honda', 
        '44' => 'toyota'
        ),
        'Job' => Array (   
        '70' => 'Bookstore',
        '73' => 'Cafe' 
        ) ,
        'City' => Array (  
        '68' => 'Wisconsin',
        '63' => 'Chicago' ,
        '47' => 'New York City', 
        '46' => 'Los Angeles'
        ) 
    ) ;
    echo "output in print_r( ):<pre>";
    print_r($arrayToPrint);
    echo "</pre>";
    echo "output in echo:<br/>";
    foreach($arrayToPrint as $arrayKey=> $arrayValue1) {
        echo '<pre>';
        echo $arrayKey;
            foreach($arrayValue1 as $leafNodeValue) {
                echo '<pre>    '.$leafNodeValue.'</pre>';
            }
        echo '</pre>';
    }
    ?>

Comments

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.