0

I need to to sort an array based on how I sorted a different array, here are 2 example arrays that I have that are linked based on their indexes (so $array1[0] and $array2[0] are relevant to eachother):

$array1 = Array (
    '2',
    '1',
    '3'
);

$array2 = Array (
    'linked_to_2',
    'linked_to_1',
    'linked_to_3',
);

As you can see $array1[0] is related to $array2[0], $array1[1] to $array2[1] and $array1[2] to $array2[2].

Now I create a new that uses the same values $array1 has but ordered like this:

$sorted_array1 = Array (
    '1',
    '2',
    '3'
);

But I can't sort $array2 using the same method so how do I use $array1 and/or $sorted_array1 to create a sorted array using the values of $array2 like this:

$sorted_array2 = Array (
    'linked_to_1',
    'linked_to_2',
    'linked_to_3',
);

For a bit of context in my actual use case I have a couple of ID's in the 1st array that I query against a database and in that database table with these ID's I order them by date of creation (which is another row in that db table). The values of array2 are not in this database table and can therefore not be sorted the same way.

kevinfromspace
  • 153
  • 1
  • 15
  • Did you check this question before? https://stackoverflow.com/q/348410/1066240 – biesior Apr 14 '21 at 19:00
  • `$array1` has unique values? – nice_dev Apr 14 '21 at 19:10
  • @biesior yes I did, although the questions are similar I couldn't get a working solution for my issue from the answers posted there – kevinfromspace Apr 15 '21 at 07:02
  • @nice_dev Yes, they are ID's for an order system and every single one is unique, the $array2 values arre also unique – kevinfromspace Apr 15 '21 at 07:03
  • @kevinfromspace First step is to think in terms of usort() and creating 2 associative arrays to make order decisions during sorting – nice_dev Apr 15 '21 at 07:40
  • @kevinfromspace if you showed us what if you try and show some sample and say where did you fail it would e probably easier to give you some better clue, how it can be solved or fixed. It can be done in a quite simple way with a combination of `usort()` AND/OR common array iteration assuming that key pattern "*X = linked_to_X*" just by creating a new array of links after ordering the first array. The additional question also is if you gonna always sort first in some like natural order like 1,2,3, or it can be other? – biesior Apr 15 '21 at 09:45
  • @NicoHaase biesior send the same link, I saw that post it but couldn't fix my issue through there. Our titles are very similar but that might be because I'm not great at consisely explaining my questions usually – kevinfromspace Apr 15 '21 at 14:30

1 Answers1

0

I figured it out! Its kinda cheating because I chose to save them as associative arrays first ([1] => 1, [2] => 2, etc) but 'converting' it back to a normal array isn't too hard to do

$sorted_array2 = [];
$x = -1;
foreach($array1 as $arr1) {
    $x++;

    $newIndex = array_search($arr1, $sorted_array1);
    $sorted_array2[$newIndex] = $array2[$x]; 

}
ksort($sorted_array2);

$x is used to get the current loops 'index'. Start off as -1 and $x++ at the start of every loop.

$newIndex finds a certain index of the current loops value of $array1 inside of $sorted_array1 and takes the index.

$sorted_array2[$newIndex] = $array2[$x] places the correct value of $array2 at the same index of $sorted_array2 that keeps it linked with $sorted_array1.

ksort($sorted_array2) sorts the indexes, when saving $sorted_array2 it places them in whatever order it finds first (for example first [2], then [3] and then [1]). After ksort both arrays are linked up by index again.

Thanks to @nice_dev for giving me the idea of making them associative arrays first. Hopefully this helps someone else too.

kevinfromspace
  • 153
  • 1
  • 15