0

I'm a newbie learning PHP. Today i have a project and being stuck, it broken my brand :(. Hope anybody can help me solved it.

Example i have a Array with construct :

Array ( 
    [a] => Array ( 
        [__base_val] => A 
        [c] => Array ( 
            [__base_val] => C 
            [e] => E 
            [f] => F 
        ) 
        [d] => D 
    ) 
    [b] => Array ( 
        [__base_val] => B 
        [g] => G 
    ) 
    [h] => H 
)

Now i need get value if each node and print out a tree of value. But if using foreach function, i only can get one or two or any index of dimension if i know before.

How i can write a php code for get value follow index dimensions of array and i don't need to know the number of dimension ?

This is result i want:

class1: value from [__base_val]
class2: value from [__base_val]
child class 2 : value
uzyn
  • 6,625
  • 5
  • 22
  • 41
Rueta
  • 3,317
  • 2
  • 23
  • 22
  • I'm not sure what you really want, but you can try `array_walk_recursive()`. – uzyn Jul 23 '12 at 10:14
  • yes, i'm lower English so much.... I need show the list of catalog tree which children node and parents. I'm convert it to Array with multi dimension but don't know how to show it follow this. because in real, i can't know how many dimension array have. – Rueta Jul 23 '12 at 10:19

1 Answers1

1

I'm not sure if this is what you're looking for, but I hope it is.

<?php
function print_deep($value, $key) {
    echo "$value: value from $key\n";
}

// Note: $array being the input array
array_walk_recursive($array, 'print_deep');
uzyn
  • 6,625
  • 5
  • 22
  • 41
  • i think you right! but how do i detect level of it. For Example: Embed result into `UL LI` html form? – Rueta Jul 23 '12 at 10:56
  • 1
    For that case you may want to refer to this: http://stackoverflow.com/questions/7889788/php-recurse-multidimensional-array-while-keeping-hold-of-depth-and-key – uzyn Jul 23 '12 at 11:04
  • Thanks uzyn ! I think this is hardcode :). Other way can solved my problem, merge your solution : http://stackoverflow.com/questions/6137315/php-multidimensional-array-to-unordered-list-building-up-url-path – Rueta Jul 23 '12 at 11:08