Home  >  Article  >  Backend Development  >  php array_unique之后json_encode需要注意_php技巧

php array_unique之后json_encode需要注意_php技巧

WBOY
WBOYOriginal
2016-05-17 09:21:46842browse

例如: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]

Statement:
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