Does js EVAL function reposition items?

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?

+5
source share
3 answers
 <?php echo json_encode($array)?> 

Since the array is sparse, this allows

 {"148":"Plane","149":"Car"} 

which is the order of the properties of the object and the object is not guaranteed in JS.

http://php.net/manual/en/function.json-encode.php

Note: When encoding an array, if the keys are not a continuous numerical sequence starting from 0, all keys are encoded as strings and are explicitly specified for each pair of key values.

You can solve this by creating an array from an object, for example:

 var obj = <?php echo json_encode($array)?>; // note, eval not needed var arr = []; Object.keys(obj).forEach(function(key) { arr[key] = obj[key]; }); 

Regarding the update:

You need to save the key order separately.

 var order = <?php echo json_encode(array_keys($array))?>; var obj = <?php echo json_encode($array)?>; order.forEach(function(key) { console.log(key, obj[key]); // or whatever you need }); 

You can even build an ordered map (which arrays of PHP are actually, unlike arrays in JS) if you use ES6 or polyfill .

+8
source

Early posters already answered the question. Just add to it:

Many people get confused because they think of Javascript Objects as associative arrays in PHP. However, this is not at all true. Although it is true that we can (kind of) mimic a data structure close to an associative PHP array using objects in Javascript, they are completely different data structures and do not work the same way as arrays.

In arrays, the integrity of the index position is important in terms of data structure and index relations, so their order is maintained. However, the same rule does not matter for objects, because their "pseudo-named index" (which is really just the name of the property) is location-independent. It can exist in any order, as long as this property still has the same value assigned to it.

Hope this helps.

+2
source

There are two types of JSON data structures that you must distinguish here. Make sure the JSON parser puts your data in the structure you need. I would suggest that it probably puts it in an object , not an array .

Plagiarized directly from this answer : From RFC 7159 - JavaScript data exchange format (JSON) (emphasis mine):

An object is an unordered array containing zero or more name / value pairs, where name is a string and value is a string, number, boolean, zero, object or array.

An array is an ordered sequence with a zero or a large value.

The terms "object" and "array" come from JavaScript conventions.

And further, quoting this answer :

The order of the elements in the array ( [] ) is preserved. The order of elements (name: pairs of values) in the "object" ( {} ) is not, and usually they will be "confused" if not with the JSON format / parser itself, and then with specific language objects (Dictionary, NSDictionary, Hashtable, etc. .d.) that are used as an internal representation.

+1
source

Source: https://habr.com/ru/post/1241491/


All Articles