php 按列值对关联数组的关联数组中的行进行分组,并保留原始的第一级键

vwhgwdsa  于 2023-01-29  发布在  PHP
关注(0)|答案(7)|浏览(127)

我有一个子数组的数组,格式如下:

[
    'a' => ['id' => 20, 'name' => 'chimpanzee'],
    'b' => ['id' => 40, 'name' => 'meeting'],
    'c' => ['id' => 20, 'name' => 'dynasty'],
    'd' => ['id' => 50, 'name' => 'chocolate'],
    'e' => ['id' => 10, 'name' => 'bananas'],
    'f' => ['id' => 50, 'name' => 'fantasy'],
    'g' => ['id' => 50, 'name' => 'football']
]

我想根据每个子数组中的id字段将其分组到一个新数组中。

array
(
    10 => array
          (
            e => array ( id = 10, name = bananas )
          )
    20 => array
          (
            a => array ( id = 20, name = chimpanzee )
            c => array ( id = 20, name = dynasty )
          )
    40 => array
          (
            b => array ( id = 40, name = meeting )
          )
    50 => array
          (
            d => array ( id = 50, name = chocolate )
            f => array ( id = 50, name = fantasy )
            g => array ( id = 50, name = football )
          )
)
yzckvree

yzckvree1#

$arr = array();

foreach ($old_arr as $key => $item) {
   $arr[$item['id']][$key] = $item;
}

ksort($arr, SORT_NUMERIC);
xvw2m8pv

xvw2m8pv2#

foreach($array as $key => $value){
   $newarray[$value['id']][$key] = $value;
}

var_dump($newarray);

小菜一碟)

of1yzvn4

of1yzvn43#

下面的代码改编了@Tim库珀的代码,以减少内部数组之一不包含id时的Undefined index: id错误:

$arr = array();

foreach($old_arr as $key => $item)
{
    if(array_key_exists('id', $item))
        $arr[$item['id']][$key] = $item;
}

ksort($arr, SORT_NUMERIC);

但是,它将删除没有id的内部数组。
例如

$old_arr = array(
    'a' => array ( 'id' => 20, 'name' => 'chimpanzee' ),
    'b' => array ( 'id' => 40, 'name' => 'meeting' ),
    'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
    'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
    'e' => array ( 'id' => 10, 'name' => 'bananas' ),
    'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
    'g' => array ( 'id' => 50, 'name' => 'football' ),
    'h' => array ( 'name' => 'bob' )
);

将完全删除'h'数组。

abithluo

abithluo4#

您也可以使用ouzo-goodies中的Arrays::groupBy():

$groupBy = Arrays::groupBy($array, Functions::extract()->id);

print_r($groupBy);

结果:

Array
(
    [20] => Array
        (
            [0] => Array
                (
                    [id] => 20
                    [name] => chimpanzee
                )

            [1] => Array
                (
                    [id] => 20
                    [name] => dynasty
                )

        )

    [40] => Array
        (
            [0] => Array
                (
                    [id] => 40
                    [name] => meeting
                )

        )

    [50] => Array
        (
            [0] => Array
                (
                    [id] => 50
                    [name] => chocolate
                )

            [1] => Array
                (
                    [id] => 50
                    [name] => fantasy
                )

            [2] => Array
                (
                    [id] => 50
                    [name] => football
                )

        )

    [10] => Array
        (
            [0] => Array
                (
                    [id] => 10
                    [name] => bananas
                )

        )

)

这里是ArraysFunctions的文档。

7fhtutme

7fhtutme5#

下面是一个函数,它将一个数组作为第一个参数,将一个条件(字符串或回调函数)作为第二个参数。该函数返回一个新数组,并根据请求对数组进行分组。

/**
 * Group items from an array together by some criteria or value.
 *
 * @param  $arr array The array to group items from
 * @param  $criteria string|callable The key to group by or a function the returns a key to group by.
 * @return array
 *
 */
function groupBy($arr, $criteria): array
{
    return array_reduce($arr, function($accumulator, $item) use ($criteria) {
        $key = (is_callable($criteria)) ? $criteria($item) : $item[$criteria];
        if (!array_key_exists($key, $accumulator)) {
            $accumulator[$key] = [];
        }

        array_push($accumulator[$key], $item);
        return $accumulator;
    }, []);
}

下面是给定的数组:

$arr = array(
    'a' => array ( 'id' => 20, 'name' => 'chimpanzee' ),
    'b' => array ( 'id' => 40, 'name' => 'meeting' ),
    'c' => array ( 'id' => 20, 'name' => 'dynasty' ),
    'd' => array ( 'id' => 50, 'name' => 'chocolate' ),
    'e' => array ( 'id' => 10, 'name' => 'bananas' ),
    'f' => array ( 'id' => 50, 'name' => 'fantasy' ),
    'g' => array ( 'id' => 50, 'name' => 'football' )
);

以及将函数与字符串和回调函数一起使用的示例:

$q = groupBy($arr, 'id');
print_r($q);

$r = groupBy($arr, function($item) {
    return $item['id'];
});
print_r($r);

两个示例中的结果相同:

Array
(
    [20] => Array
        (
            [0] => Array
                (
                    [id] => 20
                    [name] => chimpanzee
                )

            [1] => Array
                (
                    [id] => 20
                    [name] => dynasty
                )

        )

    [40] => Array
        (
            [0] => Array
                (
                    [id] => 40
                    [name] => meeting
                )

        )

    [50] => Array
        (
            [0] => Array
                (
                    [id] => 50
                    [name] => chocolate
                )

            [1] => Array
                (
                    [id] => 50
                    [name] => fantasy
                )

            [2] => Array
                (
                    [id] => 50
                    [name] => football
                )

        )

    [10] => Array
        (
            [0] => Array
                (
                    [id] => 10
                    [name] => bananas
                )

        )

)

在上面的示例中,传递回调函数有些多余,但是当您传入对象数组、多维数组或您想要根据其进行分组的任意对象时,使用回调函数会有用武之地。

erhoui1w

erhoui1w6#

也许值得一提的是你也可以使用php array_reduce函数

$items = [
    ['id' => 20, 'name' => 'chimpanzee'],
    ['id' => 40, 'name' => 'meeting'],
    ['id' => 20, 'name' => 'dynasty'],
    ['id' => 50, 'name' => 'chocolate'],
    ['id' => 10, 'name' => 'bananas'],
    ['id' => 50, 'name' => 'fantasy'],
    ['id' => 50, 'name' => 'football'],
];

// Grouping
$groupedItems = array_reduce($items, function ($carry, $item) {
    $carry[$item['id']][] = $item;
    return $carry;
}, []);
// Sorting
ksort($groupedItems, SORT_NUMERIC);

print_r($groupedItems);

https://www.php.net/manual/en/function.array-reduce.php

tuwxkamq

tuwxkamq7#

由于PHP的排序算法是如何处理多维数组的--它按大小排序,然后一次比较一个元素,因此实际上可以在重构之前对输入使用保留键的排序。在函数式编程中,这意味着不需要将结果数组声明为变量。
代码:(Demo

asort($array);
var_export(
    array_reduce(
        array_keys($array),
        function($result, $k) use ($array) {
            $result[$array[$k]['id']][$k] = $array[$k];
            return $result;
        }
    )
);

我必须说函数式编程对于这个任务不是很有吸引力,因为第一级键必须被保留。
尽管array_walk()更加简洁,但它仍然需要将结果数组作为引用变量传递到闭包中。

asort($array);
$result = [];
array_walk(
    $array,
    function($row, $k) use (&$result) {
        $result[$row['id']][$k] = $row;
    }
);
var_export($result);

我可能会推荐一个经典的循环来完成这个任务。这个循环唯一需要做的事情就是重新排列第一级和第二级键。

asort($array);
$result = [];
foreach ($array as $k => $row) {
    $result[$row['id']][$k] = $row;
}
var_export($result);

老实说,我希望ksort()比循环前排序更有效,但我希望有一个可行的替代方案。

相关问题