PHP - 按另一个数组对多维数组进行排序
我正在尝试按另一个数组对多维数组进行排序,但到目前为止还不够。array_multisort
似乎仅适用于真正的排序。
假设我有这 2 个数组:
$order = array(2,3,1);
$data = array(
array('id' => 1, 'title' => 'whatever'),
array('id' => 2, 'title' => 'whatever'),
array('id' => 3, 'title' => 'whatever')
);
现在我想根据 $order
数组中的顺序对 $data
数组进行排序。
这就是我希望的结果:
$data = array(
array('id' => 2, 'title' => 'whatever'),
array('id' => 3, 'title' => 'whatever')
array('id' => 1, 'title' => 'whatever'),
);
我可以通过运行嵌套循环轻松完成此任务,但这不能很好地扩展(我的数组非常大,并且数组有更多字段)。
I'm trying to sort a multi-dimensional array by another array, but have so far come up short.array_multisort
seems be working only for real sorting.
Suppose I have these 2 arrays:
$order = array(2,3,1);
$data = array(
array('id' => 1, 'title' => 'whatever'),
array('id' => 2, 'title' => 'whatever'),
array('id' => 3, 'title' => 'whatever')
);
Now I would like to sort my $data
array according to the order in my $order
array.
This is what I would like the result to be:
$data = array(
array('id' => 2, 'title' => 'whatever'),
array('id' => 3, 'title' => 'whatever')
array('id' => 1, 'title' => 'whatever'),
);
I can accomplish this easily by running a nested loop, but that would not scale well (my array is pretty big, and the arrays have many more fields).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的示例中,$data 数组中的 id 是连续编号的,并从 1 开始。下面给出的代码假设情况总是如此。如果不是这种情况,则该代码不起作用。
在 http://codepad.org/YC8w0yHh 您可以看到它适用于您的示例数据。
编辑
如果上述假设不成立,则以下代码将达到相同的结果:
请参阅http: //codepad.org/Q7EcTSfs 作为证明。
通过在 $order 数组上调用 array_flip() ,它可以用于位置查找。这就像哈希表查找,在时间上是线性的,即 O(n)。你不能做得更好。
In your example the ids in the $data array are are numbered consecutively and starting at 1. The code I give below assumes this is always the case. If this is not the case, the code does not work.
At http://codepad.org/YC8w0yHh you can see that it works for your example data.
EDIT
If the assumption mentioned above does not hold, the following code will achieve the same result:
See http://codepad.org/Q7EcTSfs for proof.
By calling array_flip() on the $order array it can be used for position lookup. This is like a hashtable lookup, which is linear in time, or O(n). You cannot do better.
对于那些想要基于具有实际 ID 的数组而不是像已接受的答案中那样基于具有索引的数组对数据进行排序的人,您可以对
usort
使用以下简单的比较函数:因此,以下示例将正常工作,并且您不会收到
Undefined offset
通知和具有null
值的数组:输出:
For those of you who want to sort data based on an array with actual IDs, rather than based on an array with indexes like in the accepted answer - you can use the following simple comparison function for the
usort
:So the following example will work fine and you won't get the
Undefined offset
notices and an array withnull
values:Output:
PHP 中没有用于此目的的内置函数,我无法想到任何自定义函数可以使用 usort 来执行此操作。但在我看来,array_map 已经足够简单了,那么为什么不使用它呢?
There is no built-in function for this in PHP and i am unable to think of any custom function, which would do this using usort. But array_map is simple enough, imo, so why not use it instead?
我就会这么做。我将使用自定义 usort 函数 (arr_sort) 与 $data 数组结合使用。
This would be how I would do. I would use a custom usort function (arr_sort) in conjunction with the $data array.
您可以尝试使用 usort() 进行自定义排序。这样您就可以使用第一个数组来确定第二个数组的顺序。
You could try using a custom sort with usort(). This way you can use the first array to determine the order of the second array.