PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2015-01-04 17:11:53  修改时间:2024-11-16 21:43:18  分类:11.PHP基础  编辑

一般array_column 是获取二维数组的制定列,组成新的数组输出。

注意:可以制定第三个参数,指定输出数组的key

<?php
$arr = [
    ['id' => 11, 'name' => 'aa'],
    ['id' => 22, 'name' => 'bb',],
    ['id' => 44, 'name' => 'cc'],
    ['id' => 33, 'name' => 'dd']
];

// 示例1:
print_r(array_column($arr, 'name')); 
// 运行结果:
Array
(
    [0] => aa
    [1] => bb
    [2] => cc
    [3] => dd
)

// 示例2:
print_r(array_column($arr,'name','id')); 
// 运行结果:
(
    [11] => aa
    [22] => bb
    [44] => cc
    [33] => dd
)

// 示例3:
print_r(array_column($arr, null, 'name')); 
// 运行结果:
Array
(
    [aa] => Array
        (
            [id] => 11
            [name] => aa
        )

    [bb] => Array
        (
            [id] => 22
            [name] => bb
        )

    [cc] => Array
        (
            [id] => 44
            [name] => cc
        )

    [dd] => Array
        (
            [id] => 33
            [name] => dd
        )

)