2

是否有类似于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

4 回答 4

4

不,虽然您可以制作自己的函数,只使用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
                )

        )

)
于 2018-05-05T01:22:47.063 回答
0

哦,我刚刚注意到您正在按 ID 汇总它们。没有一个功能,您将需要使用循环遍历输入,并使用您想要的数据填充输出数组。例如:

$output = [];

foreach($input_array as $item) {
  $output[$item['id']][] = [
     'itemID' => $item['itemID'],
     'itemName' => $item['itemName']
  ];
}
于 2018-05-05T00:54:42.230 回答
-1

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

例子

  "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"
]
于 2019-09-08T18:55:15.320 回答
-1

很老的问题,但我希望它对某人有所帮助。不幸的是,还没有本机功能,但使用 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 循环的性能明显快于原生数组函数。

于 2019-09-01T18:58:38.973 回答