Saturday, February 2, 2019

javascript - sort object by integer key in PHP




I have an array with ID's as the key and a name from a database associated with it. I use this to get statistics from PHP for output to a Google Chart generated in JavaScript (through JSON). In PHP I have the following array:



$a = [ 
'33' => 'first',
'25' => 'second',
'14' => 'last
];



If I run the following code:



foreach( $a as $key => $val )
echo "$key => $val" . PHP_EOL;


I get the following (expected) result



   33 => first
25 => second

14 => last


I use AJAX and JSON to send this to a JavaScript environment, here it becomes an object. Now when I run the following JavaScript:



for( var i in a )
console.log( i + " => " + a[i] );


I get




   14 => last
25 => second
33 => first


so my keys are interpreted as integers and sorted in JavaScript, which in this case means the array with statistics I was sending to Google Chart is matching up with the wrong labels.



I solved this by sorting the array in PHP before I read the statistics from the database. I needed the keys to stay the same though, this is what I did in PHP:




 $tmp = array_flip( $a );
asort( $tmp );
$a = array_flip( $tmp );


this gives me the array in PHP like this:



[
'14' => 'last',
'25' => 'second',

'33' => 'first'
]


so now a foreach in PHP will have the same order as a for( i in ...) in JavaScript. I was wondering if there is either a nicer way of sorting my array in PHP instead of flipping it twice -or- if there's an option for a loop in JavaScript that doesn't order the (admittedly confusing) integer value keys of my array?


Answer



Have you tried ksort?




ksort




(PHP 4, PHP 5, PHP 7) ksort — Sort an array by key



Description



bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Sorts
an array by key, maintaining key to data correlations. This is useful
mainly for associative arrays.





If it doesn't deal well with numeric-strings keys you can use uksort and write your compare function with a parseInt and spaceship operator (<=>).






If you, however, want to simply stop the reordering fully, altought you can't stop the "sorting" of the object by its keys (and note that it is not standard and is browser-dependent: not all of them will sort it), you can always use pairs inside an array to avoid changes to your original order of elements (pairs, in this case):



$a = [
['33', 'first'],
['25', 'second'],
['14', 'last']

];


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...