-2

How can I convert multidimensional array into single array

Input

[["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]]

How can I convert above input as below ?

["4|1","4|3","4|6","4|1|2","4|1|8","4|3|4","4|3|9","4|6|5","4|6|12"]
Rahul
  • 18,271
  • 7
  • 41
  • 60
  • Show us what you tried, please. (The way of doing it without any fancy array functions using simple loops is rather trivial, so you should at least have something to show.) – 04FS Sep 16 '19 at 11:50

4 Answers4

2

I see you want to flatten an array to 1-D. Here is recursive iterator class you can use,

$arr = [["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]];
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr));
$result = [];
foreach($iterator as $v) {
  $result[] = $v;
}
print_r($result);

RecursiveArrayIterator - This iterator allows to unset and modify values and keys while iterating over Arrays and Objects in the same way as the ArrayIterator. Additionally, it is possible to iterate over the current iterator entry.

Ref.

Demo

Solution 2:-

$arr = [["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]];
array_walk_recursive($arr, function($v) use(&$result){
  $result[] = $v;
});
print_r($result);

Demo

Output:-

Array
(
    [0] => 4|1
    [1] => 4|3
    [2] => 4|6
    [3] => 4|1|2
    [4] => 4|1|8
    [5] => 4|3|4
    [6] => 4|3|9
    [7] => 4|6|5
    [8] => 4|6|12
)
Rahul
  • 18,271
  • 7
  • 41
  • 60
0

You can use array_merge().

$arrays = [["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]]
$arrayMerge = []
foreach($arrays as $array)
{
    $arrayMerge = array_merge($arrayMerge, $array)
}

For more info: https://www.php.net/manual/es/function.array-merge.php

lfpp
  • 167
  • 1
  • 4
  • This results in: `array ( 0 => '4|1', 1 => '4|3', 2 => '4|6', 3 => array ( 0 => '4|1|2', 1 => '4|1|8', ), 4 => array ( 0 => '4|3|4', 1 => '4|3|9', ), 5 => array ( 0 => '4|6|5', 1 => '4|6|12', ), )` which is not what the OP desires. – Progrock Sep 16 '19 at 14:09
  • Given the input above, you could do: `array_merge(...array_merge([array_shift($in)], ...$in));` but a recursive approach would be less brittle. – Progrock Sep 16 '19 at 14:14
  • Yes, i didnt see you had differents deep level in your input. Sorry. In this case is better you use Rahul answer. – lfpp Sep 16 '19 at 14:52
0

Try this solution. I have used a couple of foreach loops to make it simple.

<?php 
$array = [["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]];

foreach ($array as $key => $value) {
    if($key == 0)
    {
        foreach ($value as $key => $v) {
            $new_array[] = $v;
        }
    }
    else
    {
        foreach ($value as $key => $s_value) {
            foreach ($s_value as $key => $s) {
                $new_array[] = $s;
            }
        }
    }
}


print_r($new_array);

 ?>

Here is the live demo for you.

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
0

Use array_walk_recursive, check Demo

$result = [];
$multidimension_array = [["4|1","4|3","4|6"],[["4|1|2","4|1|8"],["4|3|4","4|3|9"],["4|6|5","4|6|12"]]];
array_walk_recursive($multidimension_array, function($v) use (&$result) { $result[] = $v; });
print_r($result);
LF00
  • 27,015
  • 29
  • 156
  • 295