在PHP中提取特定键值以分隔数组

时间:2017-08-01 15:30:21

标签: php arrays laravel-5

数组结构就像这样

    $array1 = [
   "101" => [
        "name" => "Robin",
        "age" => "25"
    ],
    "102" => [
        "name" => "asRobin",
        "age" => "25"
    ],
    "103" => [
        "name" => "Roasbin",
        "age" => "25"
    ],
    "104" => [
        "name" => "Robiasn",
        "age" => "25"
    ]
  ]

这里我使用$ids = array_keys($array1)函数在特定数组中获取了id。现在同样如何提取名称

2 个答案:

答案 0 :(得分:4)

使用array_map

$names = array_map(function($a) { return $a["name"]; }, $array1);

答案 1 :(得分:3)

编辑: 我只记得那个功能,它提供了一种更优雅的方式:

$names = array_column($array1);