PHP indexed arrays and associative arrays

Source: Internet
Author: User
This article mainly introduces the contents of the PHP index array and associative array, has a certain reference value, now share to everyone, the need for friends can refer to

Recently encountered a bug, record

Define an array $arr = Array (' A ', ' B ', ' C ', ' d ');//To JSON string $str1 = Json_encode ($arr);//Remove a unset ($arr [1]);//Turn JSON$STR2 = Json_ Encode ($arr); Var_dump ($str 1); Echo ' <br/> '; Var_dump ($str 2);
String (+) "[" A "," B "," C "," D "]"
String "{" 0 ":" A "," 2 ":" C "," 3 ":" D "}"

The printed result shows that one is an array and one is an object.

Obviously the first one is an indexed array, the second is an associative array. Indexed array to JSON is an array, associative array to JSON is an object.

Deleting an array element directly using unset () will convert the indexed array to an associative array

To delete an element, keep an indexed method:

Unset ($arr [1]); Array_values ($arr);
Array_splice ($array, 1, 1);
Related Article

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.