Home > Backend Development > PHP Tutorial > json_encode php array_unique needs attention after json_encode

json_encode php array_unique needs attention after json_encode

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 08:44:07
Original
928 people have browsed it

For example: array_unique(array(1, 1, 2));
The result is
array(2) {
[0]=>
int(1)
[2]=>
int(2)
}
This is not a numeric array. Doing json_encode directly will output a json object instead of an array
{"0":1,"2":2}
If the js on the page needs [1,2 at this time ]This array data format may cause errors
At this time, you should make array_values ​​after array_unique
Like this: array_values(array_unique(array(1, 1, 2)));
The result is [1,2 ]

The above introduces the json_encode need to pay attention to after json_encode php array_unique, including the content of json_encode. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template