array_column函数索引问题 以及 特殊用法

  • Post author:
  • Post category:其他


$arr = [
    [0, 1],
    [2, 3],
    [4, 5],
    [6, 7],
];

$a1 = array_column($arr, '0');
$a2 = array_column($arr, 0);

print_r($a1);//Array ( )
print_r($a2);//Array ( [0] => 0 [1] => 2 [2] => 4 [3] => 6 )

特殊用法(这个在重置索引,很管用):

$goods_detail_list = [
    ['goods_id' => 2, 'nm' => '333'],
    ['goods_id' => 5, 'nm' => '555'],
    ['igoods_idd' => 7, 'nm' => '8888'],
];

$c = array_column($goods_detail_list, null, 'goods_id');

print_r($c);

/*
Array
(
    [2] => Array
        (
            [goods_id] => 2
            [nm] => 333
        )

    [5] => Array
        (
            [goods_id] => 5
            [nm] => 555
        )

    [6] => Array
        (
            [igoods_idd] => 7
            [nm] => 8888
        )

)
 */



版权声明:本文为weixin_38230961原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。