简体   繁体   English

基于数组排序数组 - php

[英]Sort array based on an array - php

I have an array of superheroes named $heroes : 我有一系列名为$heroes的超级$heroes

$heroes=array("Hulk","Spiderman","IronMan");

and I have an array of basic powers named $powers : 我有一系列名为$powers的基本$powers

$powers=array("Strong","Webs","Machine");

I would like to sort the $heroes array alphabetically, so that it displays this: 我想按字母顺序对$heroes数组进行排序,以便显示:

$heroes=array("Hulk","IronMan","Spiderman");

and with this, I would like the powers to be sorted based on the $heroes array so that it displays this: 有了这个,我希望根据$ heroes数组对权力进行排序,以便显示:

$powers=array("Strong","Machine","Webs");

I would not like to use a two dimensional array - I need them to be in seperate arrays. 我不想使用二维数组 - 我需要它们在单独的数组中。 Any ideas? 有任何想法吗?

Yes you can, that's what array_multisort() is for, little example: 是的,你可以,这就是array_multisort()的用途,小例子:

array_multisort( $heroes, SORT_ASC|SORT_STRING, $powers);

Or just plain (simpler): 或者只是简单(简单):

array_multisort( $heroes, $powers);

But it's better to make sort type and sort order explicit. 但最好是明确排序类型和排序顺序。

array_multisort($heroes,$powers);
$array3 = array_combine($array1, $array2);
asort($array3);

$array1 = array_keys($array3);
$array2 = array_values($array3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM