是否有可能在PHP中对多维数组进行排序以按其他值(即1/0/1/0/1/0)排序 [英] Is it possible to usort a multidimensional array in PHP to be ordered by alternative values, i.e 1/0/1/0/1/0
问题描述
如果您有以下PHP数组:
If you have this PHP array:
$args = array(
'a' => array(
'order' => 1,
),
'b' => array(
'order' => 0,
),
'c' => array(
'order' => 0,
),
'd' => array(
'order' => 0,
),
'e' => array(
'order' => 1,
),
);
是否存在一种使用usort(或其他)方法的方法,该方法可以按多维键的值对其进行排序,但是它们不是增量"(0,0,0,1,1),而是交替(0,1 ,0,1,0).
Is there a way to use usort (or other) method that can order it by multidimensional key's value, but instead of being "incremental" (0,0,0,1,1), they would alternate (0,1,0,1,0).
因此,以上面的数组为例,所需的顺序输出是按备用顺序"键值对键进行排序:
So using the array above as an example, the desired order output is for the keys to be ordered by alternate "order" key value:
$args = array(
'b' => array(
'order' => 0,
),
'a' => array(
'order' => 1,
),
'c' => array(
'order' => 0,
),
'e' => array(
'order' => 1,
),
'd' => array(
'order' => 0,
),
);
简而言之;现在,这些键通过称为"order"的键的值进行排序. 并交替显示值,因此分别为:0、1、0、1、0.
In short; The keys are now ordered by the value of the key called "order" and alternate in value, so they are: 0, 1, 0, 1, 0.
我根本想不通!
推荐答案
否,这是不可能的.排序意味着您可以比较两个元素并确定它们应具有的顺序.当您有两个1
元素时,查询将失败.
No, it's not possible. Ordering means that you can compare two elements and determine which order they should have. Your query fails at that when you have two 1
elements.
但是,这并不意味着它不能通过算法完成,仅是排序或排序不是正确的工具.
However, that doesn't mean that it can't be done algorithmically, only that sorting or ordering is not the right tool for the job.
这篇关于是否有可能在PHP中对多维数组进行排序以按其他值(即1/0/1/0/1/0)排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!