简体   繁体   English

根据其值对2D数组进行分组

[英]Grouping a 2D array according to its values

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. 如果我只是知道一些随机的深奥的PHP语法,感觉就像我可以立即解决的那种问题。 Any suggestions? 有什么建议? I'll try to update this is if I figure it out in the meantime. 如果我在此期间弄清楚的话,我会尝试更新这个。

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;
}

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;
}

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 我原本试图对对象数组进行排序,因此无论如何我都要编写它,这是一个可以处理对象的对象,即使它是一个有点hacky的想法

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM