tags:

views:

276

answers:

2

Hi,

Basically my app is interacting with a web service that sends back a weird multidimensional array such as:

Array
(
    [0] => Array
        (
            [Price] => 1
        )
    [1] => Array
        (
            [Size] => 7
        )
    [2] => Array
        (
            [Type] => 2
        )
)

That's not a problem, but the problem is that the service keeps changing the index of those items, so in the next array the Price could be at 1 instead of 0.

How do I effeciently transform arrays like this into a single dimension array so I can access the variables through $var['Size'] instead of $var[1]['Size']?

Appreciate your help

+4  A: 

Like this:

$result = array();

foreach($array as $inner) {
    $result[key($inner)] = current($inner);        
}

The $result array would now look like this:

Array
(
    [Price] => 1
    [Size] => 7
    [Type] => 2
)
Tatu Ulmanen
+2  A: 
$result = call_user_func_array('array_merge', $array);
stereofrog
This is both correct and slick.
chris