在php ....我怎樣才能排序下面的例子,通過「attachment_id」下每個單個數組[圖像] [videoembed] [etc]因此,[圖像]將訂購691,692,699,然後[videembed]會被類似的命令,如果有超過1 ...等等。php - 按子對象排序數組
我真的希望這是有道理的。
非常感謝大家。
(
[image] => Array
(
[0] => stdClass Object
(
[title] => 77007_433592946695465_6701985_n.jpg
[attachment_id] => 699
)
[1] => stdClass Object
(
[title] => 253163_10151127010009522_736212653_n.jpg
[attachment_id] => 692
)
[2] => stdClass Object
(
[title] => 263893_10150217397944522_4550001_n.jpg
[attachment_id] => 691
)
)
[videoembed] => Array
(
[0] => stdClass Object
(
[title] => YouTube
[attachment_id] => 692
)
)
[] => Array
(
[0] => stdClass Object
(
[title] => yada.jpg
[attachment_id] => 688
)
)
)
這事我想,但不能得到它的工作: 代碼: 功能CMP($ A,$ B){ \t \t回報$ A- > attachment_id - $ b-> attachment_id; \t \t} $ a \t = $ p-> get_attached_array(); \t \t $ newarray = array(); \t \t \t \t \t 的foreach \t($ a作爲attach_type $){// $ attach_type =子陣列的主陣列($ A)...圖像陣列,vieoembed陣列等 \t \t \t $ newarray [] = usort($ attach_type,'cmp'); \t \t} \t \t \t \t的print_r($ newarray); –
我不確定我是否正確理解你的問題,但是如果你想讓你的數組首先被「類型」(圖像,videoembed)和附件id排序,那麼首先你需要使用usort排序子數組,然後連接它們並按鍵(ksort)排序。我不確定它可以用一種方式完成,但也許有一些全局狀態的扭曲的usort可以做到這一點。 – erdeszt