-2

i am having a list of array in array which i want to convert to a single Array.

Here is my array:-

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

   [1] => Array
    (
        [0] => 13
        [1] => 9
    )

   [2] => Array
    (
        [0] => 13
        [1] => 15
    )

)

How can i get this to a single array. I have checked array_column but i don't have any key name so don't know how to use this.

Answer should be something like:

Array
(
  [0] => 13
  [1] => 9
  [2] => 13
  [3] => 15
)

Any help will be highly appreciated.

Harpreet Singh
  • 999
  • 9
  • 19

2 Answers2

0

Here is how I solved the problem.

$singleArray = array();
$multiArray = array(
    array(null),
    array(13,9),
    array(13,15)
);

foreach ($multiArray as $row) {
    foreach ($row as $val) {
        if ($val !== null) {
            $singleArray[] = $val;
        }//END IF
    }
}

echo '<pre>' . print_r($multiArray, true) . '</pre>';
echo '<pre>' . print_r($singleArray, true) . '</pre>';

Which outputs

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

    [1] => Array
        (
            [0] => 13
            [1] => 9
        )

    [2] => Array
        (
            [0] => 13
            [1] => 15
        )

)
Array
(
    [0] => 13
    [1] => 9
    [2] => 13
    [3] => 15
)
Mic1780
  • 1,774
  • 9
  • 23
0

you may use following way .

$main_arr = array(
    array(null),
    array(13,9),
    array(13,15)
);

$result_arr = array();
foreach($main_arr as $arr){
   $result_arr = array_merge($result_arr,array_values($arr));
}

Result Array Will be

$resuly_arr = array
(
    [0] => 
    [1] => 13
    [2] => 9
    [3] => 13
    [4] => 15
)
vjy tiwari
  • 843
  • 1
  • 5
  • 17