phparraysmultidimensional-array

Isolate a column of values from a 2d subarray


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 
    )
)

Solution

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