我有一個數組:重新安排PHP關聯數組
$example = array();
$example ['one'] = array('first' => 'blue',
'second' => 'red');
$example ['two'] = array('third' => 'purple',
'fourth' => 'green');
$example ['three'] = array('fifth' => 'orange',
'sixth' => 'white');
基於某些輸入到函數中,我需要改變示例數組的順序foreach循環處理我的輸出之前:
switch($type)
case 'a':
//arrange the example array as one, two three
break;
case 'b':
//arrange the example array as two, one, three
break;
case 'c':
//arrange the example array in some other arbitrary manner
break;
foreach($example as $value){
echo $value;
}
有沒有簡單的方法來做到這一點,而無需重新設計我所有的代碼?我有一個相當深入的foreach循環來處理,如果有簡單的方法可以簡單地重新排序數組,每次都會非常有用。
你的「任意數組排序?」的目的是什麼?你只是試圖隨機化訂單?或者,你是否特別想以參數化的方式轉移項目? – user1477388 2013-02-26 21:23:37
我想問題的關鍵是我需要對它們進行排序,並且不會落入任何現成的排序標準,如字母或數字......我需要將它按照我如何分類進行排序。 – absentx 2013-02-26 21:55:24