Nebukarr Nebukarr - 4 months ago 10
PHP Question

how do i remove array in array

Sometimes, when I fetch something, I get the following array:

Array (
[0] => Array (
[0] => 3803
)
[1] => Array (
[0] => 3855
)
[2] => Array (
[0] => 3855
)
[3] => Array (
[0] => 3855
)
[4] => Array (
[0] => 3856
)
)


But, what I actually need is:

Array ( [0] => 3803 [1] => 3855 [2] => 3855 [3] => 3855 [4] => 3856 )


So, I do this:

$new_array = array();

foreach ($array as $data)
{
array_push($new_array,$data[0]);
}


I'm not sure, if there is a better way. Maybe without making a new array?

Thanks for your thoughts!

Answer

This is the shortest way. Try this:

$array = array(
    array("3803"),
    array("3855"),
    array("3855"),
    array("3855"),
    array("3856")
);

$result = call_user_func_array('array_merge', $array);

Output:

Array
(
 [0] => 3803
 [1] => 3855
 [2] => 3855
 [3] => 3855
 [4] => 3856
)
Comments