假设我们有这个对象数组:
$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.
非常感谢任何帮助或指示.