Home > php教程 > php手册 > php array_unique之后json_encode需要注意

php array_unique之后json_encode需要注意

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:12:35
Original
1136 people have browsed it

例如:array_unique(array(1, 1, 2));
他的结果是
array(2) {
[0]=>
int(1)
[2]=>
int(2)
}

这就不是numeric数组了,直接做json_encode,会输出一个json对象,而不是数组
{"0":1,"2":2}

如果这时候页面上js需要的是[1,2]这种数组数据格式,就有可能会产生错误

此时应该在array_unique之后,在做一个array_values
这样:array_values(array_unique(array(1, 1, 2)));

结果就是[1,2]

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template