Wednesday, 29 August 2018

How to convert a multidimensional array to a one-dimensional array in PHP?

I want convert this multi dimensional array to single dimensions array

array([ca] => Array (
       [0] => Array ( [userid] => 1 )
       [1] => Array ( [userid] => 10 )
       [2] => Array ( [userid] => 14 )
       [3] => Array ( [userid] => 16 )
       [4] => Array ( [userid] => 17 )
       [5] => Array ( [userid] => 18 )
       [6] => Array ( [userid] => 25 )
     )

Convert to following array
array ([ca] =>
        array(
        [0] => 1
        [1] => 10
        [2] => 14
        [3] => 16
        [4] => 17
        [5] => 18
        [6] => 25
     ))

Thanks in advance

$res = [];
foreach ($array as $key => $val) {
    $res['ca'][] = $val['userid'];
}

print_r($res);

0 comments:

Post a Comment