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


$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"开头的对象位于顶部.然后是键以"当前"开头的对象,最后是键以"下一个"开头的对象.

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

期望的结果是这样的:

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/
        )

)

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

我try 通过首先创建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);

但现在我失go 了我的对象,它只是一个简单的array.

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

推荐答案

以下是一种按每个对象中的第一个属性名称对对象数组进行排序的方法. 使用array_multisort()是理想的,因为它减少了需要处理函数调用的时间数量(与usort()相比).

如果您需要添加更多排序规则,请将它们添加在第1个和第2个参数array_multisort()之间.

代码 : ( Demo )

$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);

要使用"桶"进行排序,请定义桶的顺序,然后在初始化对象时填充相应的桶,然后压平桶项. 这将具有更好的时间复杂性,但我假设内存消耗更高.Demo

$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)));

Php相关问答推荐

Woocommerce经常一起购买(添加到购物车)按钮重命名

PHP Array to Powershell exec命令Args

PHP DOMDocument忽略第一个表S结束标记

创建一个新的可变产品及其属性以用于WooCommerce中的变体

闭包绑定$This和垃圾收集器

将客户重定向到自定义感谢页后的空白页

WooCommerce常规价格增加额外的附加平价

如何确保所有语言文件都使用Laravel中的新密钥进行更新?

根据WooCommerce中的客户计费国家/地区更改产品价格

使用ESI的Symfony Sulu清漆

PHP -流加密启用服务器切断字节

htaccess重命名index.php以在URL中显示为SEO的友好名称

PHP:为什么递归需要这么长时间

文本到语音:不考虑音高值

htaccess 重定向子域错误的 url

图片上传成功

PHP:数组的等边

带有分类术语数组的 WordPress Elementor 自定义查询 - 如何要求所有术语都在返回的帖子中

将样式文件或脚本引入WordPress模板

在 WooCommerce 结帐后更新用户元字段