-2

I have two PHP-arrays and i want to sort the first by using the second. for example my first array could be:

[
"Hello" => "Hallo", 
"World" => "Welt", 
"PHP" => "PHP", 
"Symfony" => "Symfony"
]

If my second array now is this:

[
    "0" => "2", 
    "1" => "1", 
    "2" => "3", 
    "3" => "0"
]

This second array always has values from 0 to n and no duplicates. I want to sort the first array by the numbers in the second so it should look like that:

[
    "Symfony" => "Symfony", 
    "World" => "Welt", 
    "Hello" => "Hallo", 
    "PHP" => "PHP"
]

How can i sort my array like that?

I asked this question before and i got referred to this question, but i can't make out the solution for my case as i don't know the keys and values in the first array. It should work for every key, so please don't just refer me to this question again because i don't get how this helps me.. Thanks

lxg95
  • 553
  • 2
  • 8
  • 28
  • Your first array is an associative array, your second is a numeric array. Quite how you are expecting to associate (pun intended) those 2 different types of array is not clear to me, can **you** explain that to me – RiggsFolly Dec 07 '20 at 14:05
  • Why `Symfony` key, which is empy `""` is replaced to `Symfony` when the other elements keep their original keys? What's the logic here? – Felippe Duarte Dec 07 '20 at 14:06
  • 1
    What have you tried? Can you explain the logic in words? While it's decipherable what the logic is, please explicitly state it. – deceze Dec 07 '20 at 14:06
  • 1
    How about you use the referred question to create something like this: `print_r(array_merge(array_flip(["Symfony", "World", "PHP", "World"]), ["Hello" => "Hallo", "World" => "Welt", "PHP" => "PHP", "Symfony" => "Symfony"]));` – Definitely not Rafal Dec 07 '20 at 14:07
  • The second array defines how the first should be sorted: the first value in the second array is a "2" so the value with index 2 in the first array should be the first(index 0) value, the second value in the second array is a "1" so the second value in the first array should be at index 1 (so it stays) and so on – lxg95 Dec 07 '20 at 14:08
  • @DefinitelynotRafal but how do i get this order? ["Symfony", "World", "PHP", "World"] – lxg95 Dec 07 '20 at 14:09
  • 2
    _“so the value with index 2 in the first array”_ - that does not exist to begin with, _because_ your first array is not numerically indexed. – CBroe Dec 07 '20 at 14:09
  • 1
    there is no sense in the explanation you provided. the logic seems to be understandable but the requested result is not compatible – vlad katz Dec 07 '20 at 14:09
  • `"Symfony" => "Symfony"` ? The initial array did not have `Symfony` as key there. It is blank. – nice_dev Dec 07 '20 at 14:12
  • @nice_dev no someone edited my Question and accidentally removed that – lxg95 Dec 07 '20 at 14:13
  • @lxg95 Ok, so second array's keys are always sequential? – nice_dev Dec 07 '20 at 14:16
  • 1
    @lxg95 Order your 2nd array by values (lets call it $orderedArray), use [array_keys](https://www.php.net/manual/en/function.array-keys.php) on your first array (lets call it $indexArray). Now loop over your $orderedArray to create a new array (lets call this $yourAnswer), while looping over $orderedArray you use the index (of the current loop element) to access the element in $indexArray[index]. – Definitely not Rafal Dec 07 '20 at 14:18
  • 2
    Ok, so somebody thought my last accurate comment was a little over the top. However I can see the edit history as can others and it was not I that messed up the `"" => "Symfony"` part of the array – RiggsFolly Dec 07 '20 at 14:28

1 Answers1

2
<?php

$a1 = [
    "Hello" => "Hallo", 
    "World" => "Welt", 
    "PHP" => "PHP", 
    "Symfony" => "Symfony",
];

$a2 = [
    "0" => "2", 
    "1" => "1", 
    "2" => "3", 
    "3" => "0",
];

//it will rearrange all arrays based on the first array order
array_multisort($a2, SORT_ASC, $a1);
print_r($a1);

Output:

Array
(
    [Symfony] => Symfony
    [World] => Welt
    [Hello] => Hallo
    [PHP] => PHP
)
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29