Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing an outer array:

Tags:

arrays

php

IF you have an array within an array, how can you remove the outer array:

$outer_array = array(0 => array(
                                'key1' => 'value1',
                                'key2' => 'value2'
     ));

print_r($outer_array) produces:

Array
(
    [0] => Array
        (
            [key1] => value1
            [key2] => value2
        )

)

Is there a function built into php so you are left with:

        Array
        (
            [key1] => value1
            [key2] => value2
        )
like image 320
john mossel Avatar asked Feb 15 '11 21:02

john mossel


1 Answers

You can simply do:

$new_array = $outer_array[0];
print_r($new_array);

Result:

Array
(
    [key1] => value1
    [key2] => value2
)

Note: As pointed out by @netcoder, to make it work for both numeric and string indexes, you can do:

$new_array = $outer_array[0];
$new_array = reset($out_arr);
like image 115
Sarfraz Avatar answered Oct 20 '22 14:10

Sarfraz