I have an array of superheroes named $heroes:

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

and I have an array of basic powers named $powers:

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

I would like to sort the $heroes array alphabetically, so that it displays this:

$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:

$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?

share

3 Answers

Yes you can, that's what array_multisort() is for, little example:

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.

share
thank you so much! – Jordan Halaby Feb 11 '12 at 19:27
array_multisort($heroes,$powers);
share
$array3 = array_combine($array1, $array2);
asort($array3);

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

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged