Why can't I retrieve the value based on the key name after turning to the array after json_encode?

Source: Internet
Author: User
After json_encode is converted into an array, why cannot the value be obtained based on the key name? Ask a white question .. PHPcode $ arr = array (34 = & gt; 1, 2 = & gt; 3); $ str = json_encode ($ arr); echo $ str; $ arr2 = (array) json_decode ($ str); why can't the value be obtained based on the key name after the echo json_encode is converted into an array?
Ask a white question ..
PHP code
  $ Arr = array (34 => 1, 2 => 3); $ str = json_encode ($ arr); echo $ str; $ arr2 = (array) json_decode ($ str ); echo'
'; Print_r ($ arr2); echo'
'; Var_dump ($ arr2 ['2']);

Why can't I retrieve the value based on the key value after the JSON encode converted from the index array is obtained?
How can I obtain the value of $ arr2 ['2?


------ Solution --------------------
$ Arr2 = json_decode ($ str, 1 );

------ Solution --------------------
By default, it is converted to an object.
------ Solution --------------------
If you want to forcibly generate a PHP join array, json_decode () requires a parameter true:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.