35

I have this kind of an array containing single-element arrays:

$array = [[88868], [88867], [88869], [88870]];

I need to convert this to one dimensional array.

Desired output:

[88868, 88867, 88869, 88870]

Is there any built-in/native PHP functionality for this array conversion?

0

8 Answers 8

114

For your limited use case, this'll do it:

$oneDimensionalArray = array_map('current', $twoDimensionalArray);

This can be more generalized for when the subarrays have many entries to this:

$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
2
  • 1
    I don't understand 'current'. I see on doc php callable $callback, but on example not see function or code for this. I apreciate some example.
    – abkrim
    Nov 8, 2015 at 17:47
  • 4
    @abkrim current() function returns the value of the current element in an array. for ex : $fruits = array("banana", "apple", "orange"); echo current($fruits) //will return "banana" since current pointer is on index 0 which is banana. Jun 2, 2016 at 10:17
20

The PHP array_merge­Docs function can flatten your array:

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

In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:

$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);

See as well: Turning multidimensional array into one-dimensional array

4

try:

$new_array = array();
foreach($big_array as $array)
{
    foreach($array as $val)
    {
        array_push($new_array, $val);
    }    
}

print_r($new_array);
3
$oneDim = array();
foreach($twoDim as $i) {
  $oneDim[] = $i[0];
}
1

Yup.

$values = array(array(88868), array(88867), array(88869), array(88870));
foreach ($values as &$value) $value = $value[0];

http://codepad.org/f9KjbCCb

0
foreach($array as $key => $value){
   //check that $value is not empty and an array
   if (!empty($value) && is_array($value)) {
      foreach ($value as $k => $v) {
         //pushing data to new array
         $newArray[] = $v;
      }
   }
}
0

For a two dimensional array this works as well:

array_merge(...$twoDimensionalArray)

0

While some of the answers on the page that was previously used to close this page did have answers that suited this question (like array_merge(...$array)). There are techniques for this specific question that do not belong on the other page because of the input data structure.

The sample data structure here is an array of single-element, indexed arrays.

var_export(array_column($array, 0));

Is all that this question requires.


If you ever have a daft job interview that asks you to do it without any function calls, you can use a language construct (foreach()) and use "array destructuring" syntax to push values into a result variable without even writing a body for the loop. (Demo)

$result = [];
foreach ($array as [$result[]]);
var_export($result);

Laravel also has a flattening helper method: Arr::flatten()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.