Sorting an array | Familiar Sight | | Join Date: Nov 2006
Posts: 161
| | |
If I have
$array1=array(3,7,12,56,89);
$array2=array(12,89,3,7,56);
Is it possible to sort $array1 so that it's in the same order as $array2? Ie. so $array1=array(12,89,3,7,56);
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: Sorting an array Quote:
Originally Posted by beary If I have
$array1=array(3,7,12,56,89);
$array2=array(12,89,3,7,56);
Is it possible to sort $array1 so that it's in the same order as $array2? Ie. so $array1=array(12,89,3,7,56); Could you explain why? It seems to me you want 2 arrays with exactly the same content.. which seems slightly wasteful.
| | Familiar Sight | | Join Date: Nov 2006
Posts: 161
| | | re: Sorting an array Quote:
Originally Posted by Markus Could you explain why? It seems to me you want 2 arrays with exactly the same content.. which seems slightly wasteful. Sure Markus,
array1 is an array of files
I have then had to do some mapping to get the correct order to display each of the files.
After that mapping, it turns out the correct order is the order of array2.
I can't just "refill" array1 with the actual files, so I need to sort array1 in the order of array2 before I display them. (There is no "natural" order to array2's files; it's not like they're alphabetically ordered or anything - that would make it easy)
Does this make sense?
I'm sure php can somehow do it. I have spent a number of hours on this, but have not had any success yet, and I decided it was time to get some more knowledgable people to help me :)
| | Expert | | Join Date: Apr 2006
Posts: 512
| | | re: Sorting an array Quote:
Originally Posted by beary If I have
$array1=array(3,7,12,56,89);
$array2=array(12,89,3,7,56);
Is it possible to sort $array1 so that it's in the same order as $array2? Ie. so $array1=array(12,89,3,7,56); you can just assign array1 the values of array2 -
$array1=array(3,7,12,56,89);
-
print_r($array1);
-
$array2=array(12,89,3,7,56);
-
print_r($array2);
-
$array1=$array2;
-
print_r($array1);
-
| | Familiar Sight | | Join Date: Nov 2006
Posts: 161
| | | re: Sorting an array Quote:
Originally Posted by ghostdog74 you can just assign array1 the values of array2 Thanks ghostdog,
I wish it were that easy... I already tried that. It breaks the whole thing. I think it's because the numbers are actually files. So I have this array of files that I need to sort, and so just making array1 (the files) = array2( the numbers representing the files) replaces files with numbers and it breaks. I really do need to be able to sort the original array1.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|