unset array Json_encode data format

Source: Internet
Author: User

I have a very interesting question today.

$arr = Array (Array (1,23), Array (1,23), Array (1,23)), Var_dump ($arr);p Rint_r (Json_encode ($arr)); Echo ' <br/> '; foreach ($arr as $k = + $v) {if ($k = = 1) {unset ($arr [$k]);}} Var_dump ($arr);p Rint_r (Json_encode ($arr));d ie;

The result of this code execution is



Can see unset after the Json_encode data format has been changed, studied, probably because the array is removed from the middle of the element in order to ensure that the array of elements in order to be added to the number key, so that to confirm the removal of an element two do not change the key can see the green part, then how to solve it Just one line of code to fix

After foreach, add the

$arr = Array_values ($arr);

It's good to index the array again.

unset array Json_encode data format

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.