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
2
  • I'm not sure what you really want, but you can try array_walk_recursive(). Commented Jul 23, 2012 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. Commented Jul 23, 2012 at 10:19

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

3 Comments

i think you right! but how do i detect level of it. For Example: Embed result into UL LI html form?
For that case you may want to refer to this: stackoverflow.com/questions/7889788/…
Thanks uzyn ! I think this is hardcode :). Other way can solved my problem, merge your solution : stackoverflow.com/questions/6137315/…

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.