Jump to content

create array key from value


ayok

Recommended Posts

One more question.

I have this array:

Array ( 0 => Array ( [width] => 500 ), 1 => Array ( [height] => 300 ),  2 => Array ( [depth] => 500) )

How can I make to:

Array([width] => 500 [height] => 300) [depth] => 500)

?

 

I can do of course with array_merge(array[0],array[1],array[2]) or array[0] + array[1] + array[2], but the amount of arrays would be different. I got error if there are only 2 arrays.

 

What could be the trick? Any help would be appreciated.

Ayok

There are several ways you could achieve this. Here is one

function reKeyArray($inputArray)
{
    if(!is_array($inputArray)) { return false; }
    $outputArray = array();
    foreach($inputArray as $subArray)
    {
        if(!is_array($subArray)) { continue; }
        $outputArray = array_merge($outputArray, $subArray);
    }
    return $outputArray;
}

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.