Note that there are some explanatory texts on larger screens.

plurals
  1. POsorting functions don't sort the array correctly
    primarykey
    data
    text
    <p>I want to sort whole bunch of keys in an array. The key-value pairs are of a large number and a name. It sorts arrays incorrectly on small numbers and it gets even worse when numbers get too big to be stored as integers. If the integers are too big they are stored as strings in an array. And those strings don't sort at all staying in the same place. </p> <p>All i want to do is to sort the Key-value pairs from largest to smallest. I have no idea how to do that since the sorting functions fail completely.<br> I'll appreciate any help I can get. </p> <p>Here is my code: </p> <pre><code>&lt;?php $encodedNames = $_GET['names']; //names array from a html input encoded with JSON.stringify(nameArray); $names=json_decode($encodedNames); //decode the names $namesLegacy = (array) NULL; //array that will store key-value pairs (number=&gt;name) //for each name gets a large number and assigns it as a key foreach($names as $name){ $name = trim($name); array_push($namesLegacy, array(getFameNumber($name)=&gt;$name)) ; } echo var_dump($namesLegacy) . "&lt;br /&gt;";//dumps the array before it is sorted krsort($namesLegacy); echo var_dump($namesLegacy) . "&lt;br /&gt;";//dumps the array after its sorted //simply prints each name in a list format for ($i = 0; $i &lt; sizeof($namesLegacy); $i++) { $keys = array_values($namesLegacy[$i]); echo $i+1 .". ".$keys[0] . "&lt;br/&gt;" ; } //gets the large number that will become a key and returns it , as a string ? function getFameNumber($name) { $resultTagId = "resultStats"; $name = str_replace(" ", "+", trim($name)); $url='url that will return the large number'; /*can be tested with google url $url='http://www.google.com/search?q='.$name.'&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t';*/ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec ($ch); curl_close ($ch); $dom = new DOMDocument(); @$dom-&gt;loadHTML( $data ); $resultsTag = $dom-&gt;getElementById($resultTagId)-&gt;nodeValue; $results = preg_replace("/[^0-9]/","",$resultsTag); return $results; } ?&gt; </code></pre> <p><strong>Update:</strong> I have run more examples to test if it will sort all non string values correctly and it turns out that it doesnt. (where previously it has worked now it fails) I have an array</p> <pre><code>array(3) { [0]=&gt; array(1) { [1800000000]=&gt; string(6) "Robert" } [1]=&gt; array(1) { [591000000]=&gt; string(6) "albert" } [2]=&gt; array(1) { [1100000000]=&gt; string(4) "Anna" } } </code></pre> <p>and when i run krsort() on it it returns </p> <pre><code>array(3) { [2]=&gt; array(1) { [1100000000]=&gt; string(4) "Anna" } [1]=&gt; array(1) { [591000000]=&gt; string(6) "albert" } [0]=&gt; array(1) { [1800000000]=&gt; string(6) "Robert" } } </code></pre> <p>which is not the correct result correct result is 1800000000, 1100000000, 591000000</p> <p>I tried to converting those string into floats but it mostly made them come out as negative numbers. So i tried going the other way converting small ints to strings, but the strange thing is that they were already strings. It seems that they are converted to ints when i form a key value pair inside an array. </p> <p>anybody knows what is going on? am i using krsort incorrectly is the problem still because the numbers are too big?</p> <p>also I will give an example of how it looks like when i have a really big number in my array</p> <pre><code>array(1) { [0]=&gt; array(1) { ["4430000000"]=&gt; string(3) "son" } } </code></pre> <p>notice the "" marks around the number where in previous arrays its not there </p> <p><strong>Update2:</strong></p> <p>Ok so i know it has nothing to do with how big the integers are. I placed this code to test it <code>$results = substr($results, 0, -3);</code> but i still get incorrect results </p> <pre><code>array(3) { [2]=&gt; array(1) { [1550]=&gt; string(5) "Reeta" } [1]=&gt; array(1) { [1800000]=&gt; string(6) "Robert" } [0]=&gt; array(1) { [1090000]=&gt; string(4) "Anna" } } </code></pre> <p>I also reversed keys and values and tried sorting with <code>arsort()</code> with same results</p> <p>If anyone could explain why strings get converted to ints when they are set as keys in the arrays that would be greatly appreciated as well. </p> <p>here is a <a href="http://php.net/manual/en/function.krsort.php" rel="nofollow">krsort</a> manual. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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