如何在PHP中将多维数组转换为单维数组?

时间:2017-02-25 17:54:42

标签: php arrays sorting multidimensional-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 ) 
     )

转换为以下数组

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

提前致谢

2 个答案:

答案 0 :(得分:1)

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

print_r($res);

答案 1 :(得分:0)

使用array_walk的一个例子:

array_walk(
    $array['ca'],
    function(&$item, $key) {
        return $item = $item['userid'];
    }
);