在php中有一个类似于array_column的函数用于多维数组

时间:2018-05-05 00:49:00

标签: php arrays multidimensional-array array-column

对于多维数组,是否有与array_column类似的函数?是否有一个函数将下面的第一个数组转换为第二个数组:

Array
(
    [0] => Array
        (
            [foodType] => fruits
            [itemID] => 1
            [itemName] => apple
        )

    [1] => Array
        (
            [foodType] => fruits
            [itemID] => 2
            [itemName] => banana
        )

    [2] => Array
        (
            [foodType] => veggies
            [itemID] => 3
            [itemName] => carrot
        )

    [3] => Array
        (
            [foodType] => veggies
            [itemID] => 4
            [itemName] => broccoli
         )

)

产生的数组:

Array
(
    [fruits] => Array
        (
            [0] => Array
                (
                    [itemID] => 1
                    [itemName] => apple
                )

            [1] => Array
                (
                    [itemID] => 2
                    [itemName] => banana
                )

        )

    [veggies] => Array
        (
            [0] => Array
                (
                    [itemID] => 3
                    [itemName] => carrot
                )

            [1] => Array
                (
                    [itemID] => 4
                    [itemName] => broccoli
                 )
        )

)

4 个答案:

答案 0 :(得分:0)

哦,我刚注意到你是按照身份汇总它们的。这不是一个功能,您需要使用循环迭代输入,并使用您想要的数据填充输出数组。例如:

$output = [];

foreach($input_array as $item) {
  $output[$item['id']][] = [
     'itemID' => $item['itemID'],
     'itemName' => $item['itemName']
  ];
}

答案 1 :(得分:0)

不,没有一个函数可以原生地获得你期望的输出,虽然你可以自己创建函数,只需使用array_column来获取类型/列,然后遍历你的数组,在匹配上删除项目不重复迭代。

类似的东西:

<?php
$data = [
    ['foodType' => 'fruits', 'itemID' => 1, 'itemName' => 'apple'],
    ['foodType' => 'fruits', 'itemID' => 2, 'itemName' => 'banana'],
    ['foodType' => 'veggies', 'itemID' => 3, 'itemName' => 'carrot'],
    ['foodType' => 'veggies', 'itemID' => 4, 'itemName' => 'broccoli']
];

function array_column_multi ($array, $column) {
    $types = array_unique(array_column($array, $column));

    $return = [];
    foreach ($types as $type) {
        foreach ($array as $key => $value) {
            if ($type === $value[$column]) {
                unset($value[$column]);
                $return[$type][] = $value;
                unset($array[$key]);
            }
        }
    }
    return $return;
}


print_r(array_column_multi($data, 'foodType'));

https://3v4l.org/KQVeN

<强>结果:

Array
(
    [fruits] => Array
        (
            [0] => Array
                (
                    [itemID] => 1
                    [itemName] => apple
                )

            [1] => Array
                (
                    [itemID] => 2
                    [itemName] => banana
                )

        )

    [veggies] => Array
        (
            [0] => Array
                (
                    [itemID] => 3
                    [itemName] => carrot
                )

            [1] => Array
                (
                    [itemID] => 4
                    [itemName] => broccoli
                )

        )

)

答案 2 :(得分:0)

很老的问题,但我希望它能对别人有所帮助。不幸的是,还没有本机函数,但是可以使用php的 array_filter()

实现输出
$foods = [
    [
        'foodType' => 'fruits',
        'itemID' => 1,
        'itemName' => 'apple',
    ],

    [
        'foodType' => 'fruits',
        'itemID' => 2,
        'itemName' => 'banana',
    ],

    [
        'foodType' => 'veggies',
        'itemID' => 3,
        'itemName' => 'carrot',
    ],

    [
        'foodType' => 'veggies',
        'itemID' => 4,
        'itemName' => 'broccoli',
    ]

];

$grouped_foods = [];
$groupByColumn = 'foodType';

array_filter($foods, function ($foodItem) use(&$grouped_foods, $groupByColumn) {
    $grouped_foods[$foodItem[$groupByColumn]][] = array_filter($foodItem, function ($key) use($groupByColumn) {
        return $key != $groupByColumn;
    }, ARRAY_FILTER_USE_KEY);
});


echo "<pre>";
print_R($grouped_foods);
echo "</pre>";

查看实际情况:https://3v4l.org/bbX5A

免责声明:for / foreach循环的性能比本地数组函数快得多。

答案 3 :(得分:0)

我更喜欢使用以下解决方案。

示例

  "allergens" => array:5 [
    0 => array:2 [
      "id" => "10"
      "object" => array:1 [
        "allergens" => "10"
      ]
    ]
    1 => array:2 [
      "id" => "11"
      "object" => array:1 [
        "allergens" => "11"
      ]
    ]
    2 => array:2 [
      "id" => "4"
      "object" => array:1 [
        "allergens" => "4"
      ]
    ]
  ]

给出此示例,如果您想要一个仅包含allergens值的数组,请使用以下代码。

解决方案

$allergens = array_map( function ( $ar ) {
   return $ar['allergens'];
}, array_column( $allergensArr, 'object' ) );

结果

array:5 [
  0 => "10"
  1 => "11"
  2 => "4"
]