Home > Backend Development > PHP Tutorial > json_encode 后转向数组 为什么根据键名取不到值呢

json_encode 后转向数组 为什么根据键名取不到值呢

WBOY
Release: 2016-06-13 13:10:04
Original
990 people have browsed it

json_encode 后转为数组 为什么根据键名取不到值呢?
问个小白的问题。。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->$arr = array(34=>1,2=>3);
$str = json_encode($arr);
echo $str;
$arr2 = (array)json_decode($str);
echo '<pre class="brush:php;toolbar:false">';
print_r($arr2);
echo '
Copy after login
'; var_dump($arr2['2']);

为什么索引数组转化的JSON encode 过来后 按照键值取取不到值呢?
我想取到$arr2['2']的值 要怎么取呢?


------解决方案--------------------
$arr2 = json_decode($str, 1);

------解决方案--------------------
默认转成对象。
------解决方案--------------------
如果想要强制生成PHP关联数组,json_decode()需要加一个参数true:
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