从多维数组键创建一维数组

时间:2018-07-03 22:25:39

标签: php arrays

我有一个多维数组

Array 
 (
    [0] => Array
        (
            [ID] => 53
            [Lft] => 11
            [Rght] => 24
            [Title] => cafeteria
            [Description] => Cafeteria
        )

    [1] => Array
        (
            [ID] => 55
            [Lft] => 20
            [Rght] => 21
            [Title] => sanitary
            [Description] => Sanitary
        )

    [2] => Array
        (
            [ID] => 58
            [Lft] => 22
            [Rght] => 23
            [Title] => medic
            [Description] => Medic
        )

 )

如何轻松获取所有id的数组,而不必遍历每种传统方式?

1 个答案:

答案 0 :(得分:2)

我们可以使用array_column基于key从多维数组中提取一维数组。

$roles = array_column($roles, 'ID');

返回

Array (
    [0] => 53
    [1] => 55
    [2] => 58
)