javascript - Does js EVAL function change position of elements? - Stack Overflow

I have an application in PHP and JS. When I EVAL the json encoded PHP array the array sort changes. For

I have an application in PHP and JS. When I EVAL the json encoded PHP array the array sort changes. For example, if I have an array in PHP like this:

<?php 
$array = [148 => 'Plane', 149 => 'Car'];
?>

<script>
var array = eval(<?php echo json_encode($array)?>);
</script>

When I print the array in console, the elements doesn't have the same position. Do you know how can this happens?

UPDATE

Thanks for the answers but I want to keep the exactly same order in a JS structure, so I don't want to order the array by a specific field. Maybe the order get from the DB is like:

[148 => object, 155 => object, 133 => object]

I want to create an array like this in JS with the order that it has (the position e from DB and it has to be that order). Is it possible?

I have an application in PHP and JS. When I EVAL the json encoded PHP array the array sort changes. For example, if I have an array in PHP like this:

<?php 
$array = [148 => 'Plane', 149 => 'Car'];
?>

<script>
var array = eval(<?php echo json_encode($array)?>);
</script>

When I print the array in console, the elements doesn't have the same position. Do you know how can this happens?

UPDATE

Thanks for the answers but I want to keep the exactly same order in a JS structure, so I don't want to order the array by a specific field. Maybe the order get from the DB is like:

[148 => object, 155 => object, 133 => object]

I want to create an array like this in JS with the order that it has (the position e from DB and it has to be that order). Is it possible?

Share Improve this question edited Jan 26, 2016 at 14:22 Javier Núñez asked Jan 26, 2016 at 12:47 Javier NúñezJavier Núñez 6221 gold badge5 silver badges17 bronze badges 1
  • You do not need eval. var array = <?php echo json_encode($array)?>; is sufficient. – Salman Arshad Commented Jan 26, 2016 at 13:14
Add a ment  | 

3 Answers 3

Reset to default 8
<?php echo json_encode($array)?>

Since the array is sparse, this resolves to

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

which is an object and object property order is not guaranteed in JS.

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

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

You can solve this by creating an array from the object, like this:

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

Concerning the update:

You need to save the order of the keys 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 construct an ordered map (which PHP's arrays actually are, unlike the arrays in JS) if you use ES6 or a polyfill.

The earlier posters have already answered the question. Just to add to it:

Many people get confused because they think of Javascript Objects as associative arrays in PHP. However that is quite not the case. While its true that we can (sort of) simulate a data structure close to a PHP associative array by using objects in Javascript, they are totally different data structures and do not work quite the same way as arrays do.

In arrays the integrity of index position is important from a data structure and index relation perspective, which is why their order is maintained. However the same rule does not matter to objects since their "pseudo-named-index" (which really is just the property name), is not place-dependent. It can exist in any order as long as that property still has the same value assigned to it.

Hope this helps.

There are two types of JSON data structures you should distinguish here. Make sure the JSON parser is putting your data into the structure you want. I'd suggest it's probably putting it into an object, not an array.

Plagiarizing directly from this answer: From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format (emphasis mine):

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

An array is an ordered sequence of zero or more values.

The terms "object" and "array" e from the conventions of JavaScript.

And further quoting from this answer:

The order of elements in an array ([]) is maintained. The order of elements (name:value pairs) in an "object" ({}) is not, and it's usual for them to be "jumbled", if not by the JSON formatter/parser itself then by the language-specific objects (Dictionary, NSDictionary, Hashtable, etc) that are used as an internal representation.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745502198a4630467.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信