Jump to content

Array Transpose


NeilTQ

Recommended Posts

Hi Folks.  Apologies, but I'm pretty much a complete PHP newb which is why I'm really struggling with this problem that I've got.

 

I had a requirement to transpose an array that I've created and so, through some research, I found this great bit of code (sourced from http://stackoverflow.com/questions/797251/transposing-multidimensional-arrays-in-php)

 

public function flipDiagonally($arr) {

    $out = array();

    foreach ($arr as $key => $subarr) {

        foreach ($subarr as $subkey => $subvalue) {

                $out[$subkey][$key] = $subvalue;;

        }

    }

    return $out;

}

 

This does almost exactly what I want, except that instead of writing all of the transposed records to a single array, it writes them to what seems to be a collection of arrays within the same object.  I've played around with the code a bit but I can't work out how to write those arrays in to a single array within the loop.  Can anyone offer any suggestions?

 

Thanks for reading!

Link to comment
https://forums.phpfreaks.com/topic/191582-array-transpose/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.