PHP associative array and indexed array differences

Source: Internet
Author: User
Tags php definition

Not found in the explicit PHP definition of associative array/index array parsing, according to Phpdocument and Baidu some of the data and the actual code test, the associative array/index data to define the resolution. This problem is mainly encountered when the mobile phone-side iOS app offers APIs, and using an associative array to JSON can better convert the array with OC resolution.


Associative array: There is no explicit index key, starting with 0 as the index key by default.

$temp _arr = Array (

' already bought elsewhere ',

' goods do not meet demand ',

' The price is too high ',

' don't want to buy ',

' sellers have no transaction history ',

' Other reasons ',

);

$temp _arr[0] = ' already bought elsewhere ';

$temp _arr[1] = ' goods do not meet demand ';

Press Print_f () to print the array:

Array ([0] = = has been bought elsewhere [1] and the product does not meet demand [2] = = Price is too high [3]/= don't want to buy [4]/= Seller no transaction record [5] = other reason)

Convert to JSON output:

["\u5df2\u7ecf\u5728\u522b\u5904\u4e70\u5230", "\u5546\u54c1\u4e0d\u7b26\u5408\u9700\u6c42", "\u4ef7\ U683c\u592a\u9ad8 "," \u4e0d\u60f3\u4e70\u4e86 "," \u5356\u5bb6\u6ca1\u6709\u4ea4\u6613\u8bb0\u5f55 "," \u5176\u4ed6 \u539f\u56e0 "]


Indexed arrays: There are explicit index keys that form key-value pairs of relationships.

$temp _arr = Array (

' 1 ' = ' has been bought elsewhere ',

' 2 ' = ' goods do not meet demand ',

' 3 ' = ' Price is too high ',

' 4 ' = ' Don't want to buy ',

' 5 ' = ' sellers have no transaction history ',

' 6 ' = ' other reason ',

);

Press Print_f () to print the array:

Array ([1] = = has been bought elsewhere [2] and the product does not meet demand [3] = = Price is too high [4]/= don't want to buy [5]/= Seller no transaction record [6] = other reason)

Convert to JSON output:

{"1": "\u5df2\u7ecf\u5728\u522b\u5904\u4e70\u5230", "2": "\u5546\u54c1\u4e0d\u7b26\u5408\u9700\u6c42", "3": "\ U4ef7\u683c\u592a\u9ad8 "," 4 ":" \u4e0d\u60f3\u4e70\u4e86 "," 5 ":" \u5356\u5bb6\u6ca1\u6709\u4ea4\u6613\u8bb0\u5f55 " , "6": "\u5176\u4ed6\u539f\u56e0"}



PHP associative array and indexed array differences

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.