Note that there are some explanatory texts on larger screens.

plurals
  1. POSort an array based on another array in php?
    text
    copied!<p>according to this answer -> <a href="https://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array">Sort an Array by keys based on another Array?</a>, I use this function to get my another array sorted:</p> <pre><code>function sortArrayByArray($array,$orderArray) { $ordered = array(); foreach($orderArray as $key) { if(array_key_exists($key,$array)) { $ordered[$key] = $array[$key]; unset($array[$key]); } } return $ordered + $array; } </code></pre> <p>at first, I have my code like this and it works fine</p> <pre><code>$array1 = array("a","b","c"); $array2 = array("2","5","1"); $array3 = array("2","5","1"); rsort($array3); //5,2,1 for($i=0;$i&lt;3;$i++){ $customer1[$array2[$i]] = $array1[$i]; } $properOrderedArray1 = sortArrayByArray($customer1, $array3); print_r($properOrderedArray1); </code></pre> <p>but when I use some logic math like multiply, it gets any errors like it said there is data type float</p> <pre><code>//multiply $a = 100000*100000; $b = 200000*200000; $c = 300000*300000; $array1 = array("a","b","c"); $array2 = array($a,$b,$c); $array3 = array($a,$b,$c); rsort($array3); //5,2,1 for($i=0;$i&lt;3;$i++){ $customer1[$array2[$i]] = $array1[$i]; } $properOrderedArray1 = sortArrayByArray($customer1, $array3); print_r($properOrderedArray1); var_dump($array2); </code></pre> <p><strong>THE ERROR: Warning: array_key_exists(): The first argument should be either a string or an integer</strong></p> <p>so any solution for this problem? Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload