根据二维数组的值对其进行分组

发布于 2024-09-27 03:43:38 字数 2016 浏览 3 评论 0原文

我一直在尝试让一些显示逻辑表现出来并保持在它所属的位置,代码已经变成了一个有趣的小问题,很高兴为它制定一个通用的解决方案。

请耐心等待,它看起来像一堵文本墙,但我尝试使用简单的示例数据对其进行良好的格式化,因此快速浏览后应该可以理解。

如果出于某种原因这整件事是一个糟糕的主意,我需要在冒犯众神之前告诉我。

从这样的数据开始,例如:

$data = array(

    array(
        'name' => 'Dave',
        'age'  => '21',
        'city' => 'New York',
    ),
    array(
        'name' => 'Mike',
        'age'  => '19',
        'city' => 'Chicago',
    ),
    array(
        'name' => 'John',
        'age'  => '21',
        'city' => 'Chicago',
    ),
    array(
        'name' => 'Matt',
        'age'  => '19',
        'city' => 'New York',
    ),
    array(
        'name' => 'Luke',
        'age'  => '21',
        'city' => 'New York',
    ),

);

使用一个键名称数组来对数据进行分组,例如

$groups = array('city', 'age);

数据将变为:

$data = array(

    'New York' => array(
        '21' => array(
            array(
                'name' => 'Dave',
                'age'  => '21',
                'city' => 'New York',
            ),
            array(
                'name' => 'Luke',
                'age'  => '21',
                'city' => 'New York',
            ),
        ),
        '19' => array(
            array(
                'name' => 'Matt',
                'age'  => '19',
                'city' => 'New York',
            ),
        ),
    ),
    'Chicago' => array(
        '19' => array(
            array(
                'name' => 'Mike',
                'age'  => '19',
                'city' => 'Chicago',
            ),
        ),
        '21' => array(
            array(
                'name' => 'John',
                'age'  => '21',
                'city' => 'Chicago',
            ),
        ),
    ),
);

然后 我说“通用解决方案”,我的意思是我正在尝试制作一些可以将事物分组到任何嵌套级别的东西,具体取决于您要求其分组的键名称的数量。

感觉就像是只要我了解 PHP 语法的一些随机深奥的部分就可以立即解决的问题。有什么建议吗?如果我同时弄清楚的话,我会尝试更新这一点。

I've been trying to get some display logic to behave and stay where it belongs and the code has turned into an interesting little problem that'd be nice to make a general solution for.

Bear with me, it looks like a wall of text but I've tried to format it nicely with simple example data so it should be understandable after a quick skim through.

If for some reason this whole thing is a terrible idea, I need telling before I create an affront to the gods.

Starting with data like this, for example:

$data = array(

    array(
        'name' => 'Dave',
        'age'  => '21',
        'city' => 'New York',
    ),
    array(
        'name' => 'Mike',
        'age'  => '19',
        'city' => 'Chicago',
    ),
    array(
        'name' => 'John',
        'age'  => '21',
        'city' => 'Chicago',
    ),
    array(
        'name' => 'Matt',
        'age'  => '19',
        'city' => 'New York',
    ),
    array(
        'name' => 'Luke',
        'age'  => '21',
        'city' => 'New York',
    ),

);

With an array of key names by which to group the data, such as

$groups = array('city', 'age);

The data then becomes:

$data = array(

    'New York' => array(
        '21' => array(
            array(
                'name' => 'Dave',
                'age'  => '21',
                'city' => 'New York',
            ),
            array(
                'name' => 'Luke',
                'age'  => '21',
                'city' => 'New York',
            ),
        ),
        '19' => array(
            array(
                'name' => 'Matt',
                'age'  => '19',
                'city' => 'New York',
            ),
        ),
    ),
    'Chicago' => array(
        '19' => array(
            array(
                'name' => 'Mike',
                'age'  => '19',
                'city' => 'Chicago',
            ),
        ),
        '21' => array(
            array(
                'name' => 'John',
                'age'  => '21',
                'city' => 'Chicago',
            ),
        ),
    ),
);

When I say "general solution", I mean I'm trying to make something that can group things to any nesting level depending on how many of the key names you ask it to group by.

It feels like the sort of problem that I could solve instantly if I just knew some random esoteric bit of PHP syntax. Any suggestions? I'll try to update this is if I figure it out in the meantime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅语花开 2024-10-04 03:43:38
function group_array($arr, $fields) {

    if(empty($fields) || !is_array($fields)) {
        return $arr;
    }

    $newarr = array(); // so that we always return an array
    $field = array_shift($fields);

    foreach($arr as $val) {
        $newarr[$val[$field]][] = $val;
    }

    foreach(array_keys($newarr) as $key) {
        // Since we shifted one field off before, this groups by the remaining
        $newarr[$key] = group_array($newarr[$key], $fields);
    }

    return $newarr;
}
function group_array($arr, $fields) {

    if(empty($fields) || !is_array($fields)) {
        return $arr;
    }

    $newarr = array(); // so that we always return an array
    $field = array_shift($fields);

    foreach($arr as $val) {
        $newarr[$val[$field]][] = $val;
    }

    foreach(array_keys($newarr) as $key) {
        // Since we shifted one field off before, this groups by the remaining
        $newarr[$key] = group_array($newarr[$key], $fields);
    }

    return $newarr;
}
瘫痪情歌 2024-10-04 03:43:38

我知道这已经过时了,但想在其他人遇到这个问题时为他们发布一个解决方案。

foreach($data as $row) {
     $data_array[$row['city']][] = $row;
}

I know this is old but wanted to post a solution for others when they come to it.

foreach($data as $row) {
     $data_array[$row['city']][] = $row;
}
梦罢 2024-10-04 03:43:38

我最初是想对对象数组进行排序,所以既然我无论如何都要写它,这里有一个适用于对象的方法,即使这是一个有点老套的想法

function group_objects($array, $fields)
{
    if (empty($fields) || !is_array($fields)) {
        return $array;
    }

    $newArray = array();
    $field    = array_shift($fields);

    foreach ($array as $object) {
        $key = call_user_func(array($object, 'get' . ucwords($field)));
        $newArray[$key][] = $object;
    }

    foreach (array_keys($newArray) as $key) {
        $newArray[$key] = group_objects($newArray[$key], $fields);
    }

    return $newArray;
}

I was originally trying to sort arrays of objects so since I was gonna write it anyway, here's one that works on objects, even if it is a slightly hacky idea

function group_objects($array, $fields)
{
    if (empty($fields) || !is_array($fields)) {
        return $array;
    }

    $newArray = array();
    $field    = array_shift($fields);

    foreach ($array as $object) {
        $key = call_user_func(array($object, 'get' . ucwords($field)));
        $newArray[$key][] = $object;
    }

    foreach (array_keys($newArray) as $key) {
        $newArray[$key] = group_objects($newArray[$key], $fields);
    }

    return $newArray;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文