如何根据键上的特定标准对对象数组进行排序?

问题描述 投票:0回答:1

假设我们有这个对象数组:


$myWondersArray =

array (
  0 => 
  (object) array(
     'currentUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 3 - Helping Mom (Part 1/3)',
     'currentUnitPart' => 'wonderskills/starter_book_2/unit_3_helping_mom/part_1/',
  ),
  1 => 
  (object) array(
     'currentUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 3 - Helping Mom (Part 2/3)',
     'currentUnitPart' => 'wonderskills/starter_book_2/unit_3_helping_mom/part_2/',
  ),
  2 => 
  (object) array(
     'currentUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 3 - Helping Mom (Part 3/3)',
     'currentUnitPart' => 'wonderskills/starter_book_2/unit_3_helping_mom/part_3/',
  ),
  3 => 
  (object) array(
     'previousUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 2 - Sid\'s New Neighbors (Part 1/3)',
     'previousUnitPart' => 'wonderskills/starter_book_2/unit_2_sids_new_neighbors/part_1/',
  ),
  4 => 
  (object) array(
     'previousUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 2 - Sid\'s New Neighbors (Part 2/3)',
     'previousUnitPart' => 'wonderskills/starter_book_2/unit_2_sids_new_neighbors/part_2/',
  ),
  5 => 
  (object) array(
     'previousUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 2 - Sid\'s New Neighbors (Part 3/3)',
     'previousUnitPart' => 'wonderskills/starter_book_2/unit_2_sids_new_neighbors/part_3/',
  ),
  6 => 
  (object) array(
     'nextUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 4 - Food from the Garden (Part 1/3)',
     'nextUnitPart' => 'wonderskills/starter_book_2/unit_4_food_from_the_garden/part_1/',
  ),
  7 => 
  (object) array(
     'nextUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 4 - Food from the Garden (Part 2/3)',
     'nextUnitPart' => 'wonderskills/starter_book_2/unit_4_food_from_the_garden/part_2/',
  ),
  8 => 
  (object) array(
     'nextUnitDisplay' => 'WonderSkills: Starter Book 2, Unit 4 - Food from the Garden (Part 3/3)',
     'nextUnitPart' => 'wonderskills/starter_book_2/unit_4_food_from_the_garden/part_3/',
  ),
)

数组所需的顺序是键以“previous”开头的对象位于顶部。然后是键以“current”开头的对象,最后是键以“next”开头的对象。

对象本身应保持不变,但应保持各部分(在值中)的正确顺序。

期望的结果是这样的:

Array
(
    [0] => stdClass Object
        (
            [previousUnitDisplay] => WonderSkills: Starter Book 2, Unit 2 - Sid's New Neighbors (Part 1/3)
            [previousUnitPart] => wonderskills/starter_book_2/unit_2_sids_new_neighbors/part_1/
        )

    [1] => stdClass Object
        (
            [previousUnitDisplay] => WonderSkills: Starter Book 2, Unit 2 - Sid's New Neighbors (Part 2/3)
            [previousUnitPart] => wonderskills/starter_book_2/unit_2_sids_new_neighbors/part_2/
        )

    [2] => stdClass Object
        (
            [previousUnitDisplay] => WonderSkills: Starter Book 2, Unit 2 - Sid's New Neighbors (Part 3/3)
            [previousUnitPart] => wonderskills/starter_book_2/unit_2_sids_new_neighbors/part_3/
        )

    [3] => stdClass Object
        (
            [currentUnitDisplay] => WonderSkills: Starter Book 2, Unit 3 - Helping Mom (Part 1/3)
            [currentUnitPart] => wonderskills/starter_book_2/unit_3_helping_mom/part_1/

        )

    [4] => stdClass Object
        (
            [currentUnitDisplay] => WonderSkills: Starter Book 2, Unit 3 - Helping Mom (Part 2/3)
            [currentUnitPart] => wonderskills/starter_book_2/unit_3_helping_mom/part_2/
        )

    [5] => stdClass Object
        (
            [currentUnitDisplay] => WonderSkills: Starter Book 2, Unit 3 - Helping Mom (Part 3/3)
            [currentUnitPart] => wonderskills/starter_book_2/unit_3_helping_mom/part_3/
        )

    [6] => stdClass Object
        (
            [nextUnitDisplay] => WonderSkills: Starter Book 2, Unit 4 - Food from the Garden (Part 1/3)
            [nextUnitPart] => wonderskills/starter_book_2/unit_4_food_from_the_garden/part_1/
        )

    [7] => stdClass Object
        (
            [nextUnitDisplay] => WonderSkills: Starter Book 2, Unit 4 - Food from the Garden (Part 2/3)
            [nextUnitPart] => wonderskills/starter_book_2/unit_4_food_from_the_garden/part_2/
        )

    [8] => stdClass Object
        (
            [nextUnitDisplay] => WonderSkills: Starter Book 2, Unit 4 - Food from the Garden (Part 3/3)
            [nextUnitPart] => wonderskills/starter_book_2/unit_4_food_from_the_garden/part_3/
        )

)

我已经尝试了通常的 php 排序功能,但我需要的不仅仅是字母顺序。我能够生成一个包含上述所有项目的数组,但最终在这个过程中丢失了对象。我真的很想保持物品完好无损。

我尝试通过首先创建 3 个数组(一个用于上一个、当前和下一个)来创建一个新数组,如下所示:

foreach($myWondersArray as $item => $value) {
    if($value->previousUnitDisplay){
        //echo "got a previous" ;
        array_push($previousArray, $value->previousUnitDisplay, $value->previousUnitPart) ;
    }
} 

然后我将它们合并,如下所示:

$completeWondersArray = array_merge($previousArray, $currentArray, $nextArray);

但是现在我丢失了我的对象,它只是一个简单的数组。

非常感谢任何帮助或指示。

php arrays sorting object
1个回答
1
投票

这是一种按每个对象中的第一个属性名称对对象数组进行排序的方法。使用

array_multisort()
是理想的选择,因为它减少了处理函数调用所需的时间(相对于
usort()
)。

如果需要添加更多排序规则,请在

array_multisort()
的第1个和第2个参数之间添加。

代码:(演示

$priorities = array_flip(['prev', 'curr', 'next']);
array_multisort(
    array_map(fn($obj) => $priorities[substr(key((array) $obj), 0, 4)] ?? PHP_INT_MAX, $myWondersArray),
    $myWondersArray
);
var_export($myWondersArray);

要使用“存储桶”排序,请定义存储桶的顺序,然后在迭代对象时填充相应的存储桶,然后展平存储桶项目。这将具有更好的时间复杂度,但我假设内存消耗更高。 演示

$buckets = array_fill_keys(['previous', 'current', 'next'], []);
foreach ($myWondersArray as $obj) {
    $buckets[substr(key((array) $obj), 0, -11)][] = $obj;
}
var_export(array_merge(...array_values($buckets)));
© www.soinside.com 2019 - 2024. All rights reserved.