Search code examples
phparraysmultidimensional-arraytranspose

Transpose a multidimensional array with 3 levels


So I currently have an array that looks like this.

Array
(
    [name] => Array
        (
            [1] => Array
                (
                    [0] => 500aqua.jpg
                )

            [2] => Array
                (
                    [0] => 500beige.jpg
                    [1] => 500beige-zoom.jpg
                )

            [3] => Array
                (
                    [0] => P1040485.JPG
                    [1] => P1040486.JPG
                    [2] => P1040487.JPG
                )
        )

    [type] => Array
        (
            [1] => Array
                (
                    [0] => image/jpeg
                )

            [2] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [3] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                )
        )

    [tmp_name] => Array
        (
            [1] => Array
                (
                    [0] => C:\xampp\tmp\php5F9.tmp
                )

            [2] => Array
                (
                    [0] => C:\xampp\tmp\php5FA.tmp
                    [1] => C:\xampp\tmp\php5FB.tmp
                )

            [3] => Array
                (
                    [0] => C:\xampp\tmp\php60C.tmp
                    [1] => C:\xampp\tmp\php63B.tmp
                    [2] => C:\xampp\tmp\php66B.tmp
                )
        )

    [error] => Array
        (
            [1] => Array
                (
                    [0] => 0
                )

            [2] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [3] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )
        )

    [size] => Array
        (
            [1] => Array
                (
                    [0] => 133659
                )

            [2] => Array
                (
                    [0] => 148940
                    [1] => 211420
                )

            [3] => Array
                (
                    [0] => 3939511
                    [1] => 4742470
                    [2] => 4345899
                )
        )

)

And I want it to look something like this

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => 500aqua.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php5F9.tmp
                    [error] => 0
                    [size] => 133659

                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [name] => 500beige.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php5FA.tmp
                    [error] => 0
                    [size] => 148940
                )
            [1] => Array
                (
                    [name] => 500beige-zoom.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php5FB.tmp
                    [error] => 0
                    [size] => 211420
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [name] => P1040485.JPG
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php60C.tmp
                    [error] => 0
                    [size] => 148940
                )
            [1] => Array
                (
                    [name] => P1040486.JPG
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php63B.tmp
                    [error] => 0
                    [size] => 148940
                )
            [2] => Array
                (
                    [name] => P1040487.JPG
                    [type] => image/jpeg
                    [tmp_name] => C:\xampp\tmp\php66B.tmp
                    [error] => 0
                    [size] => 148940
                )
        )
)

I've tried the following code but it only seems to get each of the last 3 in the arrays.

foreach ($data as $d) {
    foreach ($d as $key => $e) {
        foreach ($e as $key2 => $f) {
            $result[$key2][$key] = $f;
        }
    }
}

Like

Array
(
    [0] => Array
        (
            [name] => P1040485.JPG
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php60C.tmp
            [error] => 0
            [size] => 3939511
        )

    [1] => Array
        (
            [name] => P1040486.JPG
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php63B.tmp
            [error] => 0
            [size] => 4742470
        )

    [2] => Array
        (
            [name] => P1040487.JPG
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\php66B.tmp
            [error] => 0
            [size] => 4345899
        )

)

Solution

  • Try this one:

    foreach ($data as $key1 => $value1) {
        foreach ($value1 as $key2 => $value2) {
            foreach ($value2 as $key3 => $value3) {
                $result[$key2][$key3][$key1] = $value3;
            }
        }
    }
    

    You forgot to include the Key on the first level ($key1): name, type etc.