1

I need to read nested arrays without knowing how the array will look.

For example;

$data = array(
        'Data1_lvl1' => array(
                            'Data1_lvl2' => "value",
                            'Data2_lvl2' => array(
                                                'Data1_lvl3' => "value"
                                            )
                        ),
        'Data2_lvl1' => 'value'
    );

Needs to be formatted to strings like:

  1. Data1_lvl1/Data1_lvl2/
  2. Data1_lvl1/Data2_lvl2/Data1_lvl3/
  3. Data2_lvl1/

But the array can be of any size with any number of nested arrays inside it.

1

4 Answers 4

2
$data = array(
        'Data1_lvl1' => array(
                            'Data1_lvl2' => "value",
                            'Data2_lvl2' => array(
                                                'Data1_lvl3' => "value"
                                            )
                        ),
        'Data2_lvl1' => 'value'
    );


function printArray($array)
{
    foreach ($array as $key=>$value)
    {
       echo $key.'/';
       if (is_array($value))
       {
          printArray($value);           
       } else {    
        echo '<br>';
        }

    }

}


printArray($data);
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to output only the name of array elements then this recursive function will do the trick.

Your data:

$data = array(
        'Data1_lvl1' => array(
                            'Data1_lvl2' => "value",
                            'Data2_lvl2' => array(
                                                'Data1_lvl3' => "value"
                                            )
                        ),
        'Data2_lvl1' => 'value'
    );

Function:

function array2str($array, $str) {
  foreach($array as $key => $val) {
    if (is_array($val) ) {
        $str .= $key . '/';
        array2str($val, $str);
    }
  }
  echo $str.'<br />';  
  return $str;
}

array2str($data);

As you can see the script does ECHO in itself with <br /> to break the line when viewing results in a browser.

Comments

1

One way would to walk recursively through array with function similar to this:

<?php
function f($d, $str = '') {
        foreach ($d as $key => $val) {
                if (is_array($val)) f($val, $str . '/' . $key); // If this element is array parse next level
                else print_r($str . '/' . $key . '/'); // Output current string or do what you need to do with it..
        }
}

$data = array(
        'Data1_lvl1' => array(
                            'Data1_lvl2' => "value",
                            'Data2_lvl2' => array(
                                                'Data1_lvl3' => "value"
                                            )
                        ),
        'Data2_lvl1' => 'value'
);

f($data);

Comments

0

with that function:

<?php
function print_tree ($data, $prefix = '', &$index = 1) {
  foreach($data as $key => $datum) {
    echo $index++ . '. ' . ($new_prefix =  $prefix . $key . '/') . PHP_EOL;
    if (is_array($datum)) {
      print_tree ($datum, $new_prefix, $index);
    }
  }
}

I get

  1. Data1_lvl1/
  2. Data1_lvl1/Data1_lvl2/
  3. Data1_lvl1/Data2_lvl2/
  4. Data1_lvl1/Data2_lvl2/Data1_lvl3/
  5. Data2_lvl1/

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.