4

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?

0

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1
array_multisort($heroes,$powers);

Comments

1
$array3 = array_combine($array1, $array2);
asort($array3);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.