1

I have this type of array data

(its just a part of the original array)

Array
(
    [out] => Array
        (
            [in1] => Array
                (
                    [d1] => data1
                    [d2] => data2
                    [d3] => data3
                    [d4] => data4
                    [d5] => data5
                    [d6] => data6
                    [d7] => data7
                    [d8] => data8
                )

            [in2] => Array
                (
                    [d9] => Array
                        (
                            [b1] => data9
                            [b2] => data10
                        )

                    [d10] => Array
                        (
                            [b1] => data11
                            [b2] => data12
                        )

                )

        )

)

how to display it like this...

out.in1.d1=data1
out.in1.d2=data2
out.in1.d3=data3
out.in1.d4=data4
out.in1.d5=data5
out.in1.d6=data6
out.in1.d7=data7
out.in1.d8=data8
out.in2.d9.b1=data9
out.in2.d9.b2=data10
out.in2.d10.b1=data11
out.in2.d11.b2=data12

I've tried this

function test_print($data, $key)
{
    echo "$key='$data'\n";
}
array_walk_recursive($array, 'test_print');

but it only prints the last key
like this

d1=data1
d2=data2

1 Answer 1

4

Not sure you can do this with a built-in PHP function. But here's a simple recursive function that will give you what you want:

function recursePrint($array, $prefix = '') {
    foreach($array AS $key => $value) {
        if(is_array($value)) {
            recursePrint($value, $prefix . $key . ".");
        } else {
            echo $prefix . $key . "=" . $value . "\n";
        }
    }
}

recursePrint($array);

Working example: http://3v4l.org/VLJVU

Sign up to request clarification or add additional context in comments.

2 Comments

I upped yours (that sounds wrong...) and downed the other guy's answer because it was incorrect - was probably spite :/
@Emissary yeah he probably thought I downvoted him (I didn't) and was retaliating. Oh well.

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.