【问题标题】:PHP array sort mutli-dimensionalPHP数组排序多维
【发布时间】:2014-11-19 21:31:04
【问题描述】:

我有一个这样结构的数组(这是 CSV 格式化它的方式):

Array(

    0 => Array(
        0 => person1
        1 => person2
        2 => person3
        //all the way to 9
    ),

    1 => Array(
        0 => id belonging to person 1
        1 => id belonging to person 2
        2 => id belonging to person 3
    ),

    2 => Array(
        0 => id belonging to person 1
        1 => id belonging to person 2
        2 => id belonging to person 3
    ),

    //all the way to 20
)

我正在尝试对一个新数组(数组)进行排序,每个索引都是与上面0 索引中的键对应的值。即,person1 指向一个数组,其中所有 id 都来自外部数组 1-20。

在索引0之后的每个数组中,都包含20个id,0属于第一个数组中的key 0。

我试图实现的结构如下所示:

Array(
    [person1] => Array(
        id belonging to person 1
        id belonging to person 1
        id belonging to person 1
    ),

    [person2] => Array(
        id belonging to person 2
        id belonging to person 2
        id belonging to person 2
    ),

    [person3] => Array(
        id belonging to person 3
        id belonging to person 3
        id belonging to person 3
    ),
)

到目前为止,我的尝试已经奏效,但是,我不得不对一些索引进行硬编码。实现所需结构的最佳解决方案是什么?

【问题讨论】:

    标签: php arrays data-structures


    【解决方案1】:

    我有点不确定这是否是你要找的……

    <?php
    
    $arr = Array(
    
        0 => Array(
            0 => "person1",
            1 => "person2",
            2 => "person3"
            //all the way to 9
        ),
    
        1 => Array(
            0 => "id belonging to person 1",
            1 => "id belonging to person 2",
            2 => "id belonging to person 3"
        ),
    
        2 => Array(
            0 => "id belonging to person 1",
            1 => "id belonging to person 2",
            2 => "id belonging to person 3"
        )
    );
    
    foreach($arr[0] AS $id=>$name)
    {
        $ids[$id] = $name;
    }
    
    foreach(array_slice($arr,1) AS $persons)
    {
        foreach($persons AS $id=>$person)
        {
            // make sure to check if $ids[$id] exist and handle it as you like.
            // if(isset($ids[$id]))
            $people[$ids[$id]][] = $person;
        }
    }
    
    
    print_r($people);
    
    ?>
    

    结果:

    Array
    (
        [person1] => Array
            (
                [0] => id belonging to person 1
                [1] => id belonging to person 1
            )
    
        [person2] => Array
            (
                [0] => id belonging to person 2
                [1] => id belonging to person 2
            )
    
        [person3] => Array
            (
                [0] => id belonging to person 3
                [1] => id belonging to person 3
            )
    
    )
    

    编辑:应该注意的是,我没有检查此人的 id 是否存在于 $ids 数组中,也没有检查是否设置过 $people。

    【讨论】:

    • @NoahMatisoff 请注意我最后所说的,它不会检查 $ids 数组是否在它最后执行 $ids[$id] 和 $people 时执行 print_r。我还进行了编辑,在其中创建了最后两个循环 foreach,因此它们的 id 不需要从 0 到 n。
    • 不错的重构。我知道它很容易出现未定义的索引错误。但这不是问题,因为数据不是动态的并且来自 CSV。
    • @NoahMatisoff 我确实在您回复时对代码进行了一些您可能错过的编辑,但我将它们全部制作为 foreach 循环,因此循环中没有错误的索引。此外,如果您希望例如 [person1] 数组中的索引代表它们在 $arr 中的索引,那么只需在 foreach 循环中使用切片执行 $pid=&gt;$persons 并在分配给 $people 数组时执行此操作 @ 987654324@。我还评论了在哪里以及如何检查循环中未设置的索引。
    • 我注意到这就是为什么我说重构不错。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多