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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
DEVOPS
  • 18,190
  • 34
  • 95
  • 118

8 Answers8

116

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);
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 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 08 '15 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. – Rafique Mohammed Jun 02 '16 at 10:17
  • In a more elegant way, as @Amir suggested: `array_merge(...$this->highlights)` is strictly equivalent – Yonn Trimoreau Aug 24 '23 at 12:29
21

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

Blake Frederick
  • 1,510
  • 20
  • 31
hakre
  • 193,403
  • 52
  • 435
  • 836
4

try:

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

print_r($new_array);
redmoon7777
  • 4,498
  • 1
  • 24
  • 26
3
$oneDim = array();
foreach($twoDim as $i) {
  $oneDim[] = $i[0];
}
marioosh
  • 27,328
  • 49
  • 143
  • 192
1

For a two dimensional array this works as well:

array_merge(...$twoDimensionalArray)

Amir Hossein Abdollahi
  • 1,253
  • 2
  • 9
  • 14
1

Yup.

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

http://codepad.org/f9KjbCCb

benesch
  • 5,239
  • 1
  • 22
  • 36
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()

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
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;
      }
   }
}