我有一个数组,如下所示:
Array
(
[0] => Array
(
[category_id] => 411
[category_name] => some text
[data] => Array
(
[1] => stdClass Object
(
[album_id] => 91689
[name] => some text
[pubDate] => December 23, 2019
)
[0] => stdClass Object
(
[album_id] => 91670
[name] => some text
[pubDate] => November 23, 2019
)
)
)
[1] => Array
(
[category_id] => 15755
[category_name] => some text
[data] => Array
(
[1] => stdClass Object
(
[album_id] => 93624
[name] => some text
[pubDate] => January 02, 2019
)
[0] => stdClass Object
(
[album_id] => 93621
[name] => some text
[pubDate] => January 02, 2019
)
)
)
[2] => Array
(
[category_id] => 15879
[category_name] => some text
[data] => Array
(
[0] => stdClass Object
(
[album_id] => 91683
[name] => some text
[pubDate] => December 31, 2019
)
)
)
[3] => Array
(
[category_id] => 456
[category_name] => some text
[data] => Array
(
[3] => stdClass Object
(
[album_id] => 93675
[name] => some text
[pubDate] => January 03, 2019
)
[2] => stdClass Object
(
[album_id] => 91679
[name] => some text
[pubDate] => November 23, 2019
)
[1] => stdClass Object
(
[album_id] => 91675
[name] => some text
[pubDate] => November 23, 2019
)
[0] => stdClass Object
(
[album_id] => 91667
[name] => some text
[pubDate] => November 23, 2019
)
)
)
[4] => Array
(
[category_id] => 435
[category_name] => some text
[data] => Array
(
[2] => stdClass Object
(
[album_id] => 93552
[name] => some text
[pubDate] => December 31, 2019
)
[1] => stdClass Object
(
[album_id] => 91685
[name] =>some text
[pubDate] => November 23, 2019
)
[0] => stdClass Object
(
[album_id] => 91677
[name] => some text
[pubDate] => November 23, 2019
)
)
)
)
我想要这样:
Array
(
[0] => Array
(
[category_id] => 411
[category_name] => some text
[data] => Array
(
[1] => stdClass Object
(
[album_id] => 91689
[name] => some text
[pubDate] => December 23, 2019
)
[0] => stdClass Object
(
[album_id] => 91670
[name] => some text
[pubDate] => November 23, 2019
)
)
)
[1] => Array
(
[category_id] => 15755
[category_name] => some text
[data] => Array
(
[1] => stdClass Object
(
[album_id] => 93624
[name] => some text
[pubDate] => January 02, 2019
)
[0] => stdClass Object
(
[album_id] => 93621
[name] => some text
[pubDate] => January 02, 2019
)
)
)
[2] => Array
(
[category_id] => 15879
[category_name] => some text
[data] => Array
(
[0] => stdClass Object
(
[album_id] => 91683
[name] => some text
[pubDate] => December 31, 2019
)
)
)
[3] => Array
(
[category_id] => 456
[category_name] => some text
[data] => Array
(
[3] => stdClass Object
(
[album_id] => 93675
[name] => some text
[pubDate] => January 03, 2019
)
[2] => stdClass Object
(
[album_id] => 91679
[name] => some text
[pubDate] => November 23, 2019
)
[1] => stdClass Object
(
[album_id] => 91667
[name] => some text
[pubDate] => November 23, 2019
)
[0] => stdClass Object
(
[album_id] => 91675
[name] => some text
[pubDate] => November 23, 2019
)
)
)
[4] => Array
(
[category_id] => 435
[category_name] => some text
[data] => Array
(
[2] => stdClass Object
(
[album_id] => 91685
[name] => some text
[pubDate] => December 31, 2019
)
[1] => stdClass Object
(
[album_id] => 91677
[name] =>some text
[pubDate] => November 23, 2019
)
[0] => stdClass Object
(
[album_id] => 93552
[name] => some text
[pubDate] => November 23, 2019
)
)
)
)
请帮忙! 谢谢!
您可以使用 usort 对内部数组进行排序,
//convert to array if not,
$arr = json_decode(json_encode($arr), true);
function custom($a, $b)
{
if(($a["album_id"] == $a["album_id"])) return 0;
return ($a["album_id"] >= $a["album_id"]) ? -1 : 1; // interchange for order change
}
foreach($arr as &$v){
uasort($v['data'], "custom");
}
演示
输出:-
Array
(
[0] => Array
(
[category_id] => 411
[category_name] => some text
[data] => Array
(
[0] => Array
(
[album_id] => 91689
[name] => some text
[pubDate] => December 23, 2019
)
[1] => Array
(
[album_id] => 91670
[name] => some text
[pubDate] => November 23, 2019
)
)
)
[1] => Array
(
[category_id] => 15755
[category_name] => some text
[data] => Array
(
[0] => Array
(
[album_id] => 93624
[name] => some text
[pubDate] => January 02, 2019
)
[1] => Array
(
[album_id] => 93621
[name] => some text
[pubDate] => January 02, 2019
)
)
)
[2] => Array
(
[category_id] => 15879
[category_name] => some text
[data] => Array
(
[0] => Array
(
[album_id] => 91683
[name] => some text
[pubDate] => December 31, 2019
)
)
)
[3] => Array
(
[category_id] => 456
[category_name] => some text
[data] => Array
(
[0] => Array
(
[album_id] => 93675
[name] => some text
[pubDate] => January 03, 2019
)
[1] => Array
(
[album_id] => 91679
[name] => some text
[pubDate] => November 23, 2019
)
[2] => Array
(
[album_id] => 91675
[name] => some text
[pubDate] => November 23, 2019
)
[3] => Array
(
[album_id] => 91667
[name] => some text
[pubDate] => November 23, 2019
)
)
)
[4] => Array
(
[category_id] => 435
[category_name] => some text
[data] => Array
(
[0] => Array
(
[album_id] => 93552
[name] => some text
[pubDate] => December 31, 2019
)
[1] => Array
(
[album_id] => 91685
[name] => some text
[pubDate] => November 23, 2019
)
[2] => Array
(
[album_id] => 91677
[name] => some text
[pubDate] => November 23, 2019
)
)
)
)