0

I want to convert this array...

Array
(
[0] => Array
    (
        [1] => A1
        [2] => A11
        [3] => A111
    )

[1] => Array
    (
        [4] => A2
        [5] => A22
    )

[2] => Array
    (
        [6] => A3
        [7] => A33
    )

 )

as

Array
(    
 [0] => A1
 [1] => A11
 [2] => A111
 [3] => A2
 [4] => A22  
 [5] => A3
 [6] => A33
)

Can any one guide me on this please?

skaffman
  • 398,947
  • 96
  • 818
  • 769
saran
  • 595
  • 5
  • 17
  • 28
  • @AndreiG - actually it didn't when I originally saw it. should be in the tags as well. – tvanfosson Mar 07 '12 at 13:22
  • possible duplicate of [Convert a nested array into a flat array with PHP](http://stackoverflow.com/questions/2538906/convert-a-nested-array-into-a-flat-array-with-php) – Jordan Running Mar 07 '12 at 13:23
  • I'm sure the "PHP" wasn't in the title when I first saw it, but the history for the post doesn't have this - are there revisions not everyone sees? – Ben Parsons Mar 07 '12 at 13:24
  • possible duplicate of [How to "flatten" a multi-dimensional array to simple one in PHP?](http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php) – Felix Kling Mar 07 '12 at 13:24

4 Answers4

4

"Flatten" is the word you're looking for - you want to flatten this array.

There is some example code in the comments of the php manual:

http://php.net/manual/en/function.array-values.php

/** 
 * Flattens an array, or returns FALSE on fail. 
 */ 
function array_flatten($array) { 
  if (!is_array($array)) { 
    return FALSE; 
  } 
  $result = array(); 
  foreach ($array as $key => $value) { 
    if (is_array($value)) { 
      $result = array_merge($result, array_flatten($value)); 
    } 
    else { 
      $result[$key] = $value; 
    } 
  } 
  return $result; 
} 
Ben Parsons
  • 1,425
  • 1
  • 14
  • 30
  • Hi Ben, thanks for you answer. I got the answers but the key values are changed and new keys are generated. How to have the old key values here? – saran Mar 07 '12 at 13:36
1

You could do something like

$flattened = call_user_func_array('array_merge', $your_array);

Note that this requires that your array indexes have numeric values in order to work properly. But it looks like that isn't a problem. :) It also won't preserve the original indexes, though -- it'll just tack on new values at the end.

cHao
  • 84,970
  • 20
  • 145
  • 172
1
$result = array_reduce($original, 'array_merge', array());

This is the equivalent of a fold in functional programming. Also see the PHP manual on array_reduce.


If you want to preserve key->value bindings, replace array_merge with this simple function:

function array_merge_with_keys($a, $b) {
  foreach($b as $key => $val) {
    $a[$key] = $val;
  }
  return $a;
}
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
1

Use this

$newarray = array();
foreach($array as $subarray) {
    foreach ($subarray as $key=>$value) {
        $newarray[$key] = $value;
    }
}
botzko
  • 630
  • 3
  • 8
  • I need their corresponding key too. – saran Mar 07 '12 at 13:28
  • Now it will save the keys from the subarray :). In this case you can miss something because $array[0][1] = 'A01' and $array[1][1] = 'A11' you will receive only $newarray[1] = 'A11' because keys in the subarray are equal. – botzko Mar 07 '12 at 13:31