4

I am Having an mutidimensional array getting result like given below

Array
(
    [0] => Array
        (
            [0] => 70
        )

    [1] => Array
        (
            [0] => 67
        )

    [2] => Array
        (
            [0] => 75
            [1] => 73
            [2] => 68
        )

    [3] => Array
        (
            [0] => 68
        )

    [4] => Array
        (
            [0] => 76
        )

)

But I need to convert it to single array

And I want to convert in to single dimensional array as

Array
(
[0] => 70
[1] => 67
[2] => 75
[3] => 73
[4] => 68
[5] => 68
[6] => 76
)

How to convert it using php functions?

Or Is there any other way to do it?

webnoob
  • 15,747
  • 13
  • 83
  • 165
Durgesh Sonawane
  • 95
  • 1
  • 3
  • 11
  • 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) – Prasanth Bendra Jul 17 '14 at 08:58

6 Answers6

15

You can try

$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$l = iterator_to_array($it, false);

var_dump($l); // one Dimensional 
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Baba
  • 94,024
  • 28
  • 166
  • 217
2

Try with:

$input  = array(/* your array*/);
$output = array();

foreach ( $input as $data ) {
  $output = array_merge($output, $data);
}
hsz
  • 148,279
  • 62
  • 259
  • 315
1

You can use array_walk_recursive() for that coupled with a closure:

$res = array(); // initialize result

// apply closure to all items in $data
array_walk_recursive($data, function($item) use (&$res) {
    // flatten the array
    $res[] = $item;
});

print_r($res); // print one-dimensional array
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

This should do the trick

$final = array();
foreach ($outer as $inner) {
  $final = array_merge($final, $inner);
}
var_dump($final);

Or you could use array_reduce() if you have PHP >= 5.3

$final = array_reduce($outer, function($_, $inner){
  return $_ = array_merge((array)$_, $inner);
});
var_dump($final);
maček
  • 76,434
  • 37
  • 167
  • 198
0

For a more generic function which can deal with multidimensional arrays, check this function,

function  arrayFlattener($input = array(), &$output = array()) {
  foreach($input as $value) {
    if(is_array($value)) {
        arrayFlattener($value, $output);
    } else {
        $output[] = $value;
    }
  }
}

You can find an example here.

Awemo
  • 875
  • 1
  • 12
  • 25
0

By using this function you can convert any dimension array into a single dimention array.

$result = array();
$data = mearchIntoarray($result,$array);
function mearchIntoarray($result,$now)
{
    global $result;
    foreach($now as $key=>$val)
    {
        if(is_array($val))
        {
            mearchIntoarray($result,$val);
        }
        else
        {
            $result[] = $val;
        }
    }
    return $result;
} 

Where $array is your given array value.

Suman Biswas
  • 853
  • 10
  • 19