33

I have this kind of an array:

Array
(
    [0] => Array
        (
            [0] => 88868
        )
    [1] => Array
        (
            [0] => 88867
        )
    [2] => Array
        (
            [0] => 88869
        )
    [3] => Array
        (
            [0] => 88870
        )
)

I need to convert this to one dimensional array. How can I do that?

For example like this..

Array
(
    [0] => 88868
    [1] => 88867
    [2] => 88869
    [3] => 88870 
)

Any php built in functionality is available for this array conversion?

mickmackusa
  • 37,596
  • 11
  • 75
  • 105
DEVOPS
  • 17,240
  • 30
  • 93
  • 117

7 Answers7

113

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
  • 491,798
  • 79
  • 706
  • 853
  • 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
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

Blake Frederick
  • 1,384
  • 16
  • 29
hakre
  • 184,866
  • 48
  • 414
  • 792
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,378
  • 1
  • 23
  • 25
3
$oneDim = array();
foreach($twoDim as $i) {
  $oneDim[] = $i[0];
}
marioosh
  • 25,903
  • 44
  • 135
  • 187
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,121
  • 1
  • 21
  • 35
0

While some of the answers on the page that was previously used to close this page did have answers that suited this question. 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.

mickmackusa
  • 37,596
  • 11
  • 75
  • 105
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;
      }
   }
}