I have an application in PHP and JS. When I EVAL a PHP encoded json array, the sorting of the array changes. For example, if I have an array in PHP, for example:
<?php $array = [148 => 'Plane', 149 => 'Car']; ?> <script> var array = eval(<?php echo json_encode($array)?>); </script>
When I print an array in the console, the elements do not have the same position. Do you know how this can happen?
UPDATE
Thanks for the answers, but I want to keep exactly the same order in the JS structure, so I do not want to order an array by a specific field. Maybe the order from the database looks like this:
[148 => object, 155 => object, 133 => object]
I want to create such an array in JS with the order that it has (the position is taken from the database, and it should be this order). Is it possible?
source share