【发布时间】:2013-02-12 12:17:55
【问题描述】:
我有一个数组:
$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 循环来进行处理,如果有一种简单的方法可以简单地每次重新排序数组,那将非常有帮助。
【问题讨论】:
-
你的“任意数组排序”的目的是什么?你只是想随机化订单吗?或者,您是否特别想以参数化方式移动项目?
-
我想更重要的是我需要按照我想要的方式对它们进行排序,并且不属于任何现成的排序标准,如字母或数字……我需要准确地排序我告诉它进行排序。
标签: php arrays multidimensional-array associative-array