Php array (1)

Source: Internet
Author: User
Php array (1) The php array index starts from 0, not 1.

$ States [0] // A normal array can use the sequence number as the key $ states ['A'] // The associated array uses a key-value pair to call $ states ['A'] [' aaa'] // This method can be used to call multiple arrays.

  1. In php, no matter whether an associated array or a numeric key is used, it depends on an array pointer. the array pointer is like a bookmark, indicating the array bit being checked.

  2. Instead of directly operating the array pointer, you use the built-in language features or functions to traverse the array.

  3. In php, all array management methods are actually operating the array pointer, but the pointer operation methods are built in php or encapsulated by functions.

Directly create an array
$states[0] = 1;$states[] = 1;$states['aa'] = 1;
Use array () to create an array
$languages = array('spain'=>'spanish','ireland'=>'english');
Extract array with list
File users.txt content a | B | c $ users = fopen('users.txt ', 'r'); while ($ line = fgets ($ users, 4096) {list ($ name, $ sex, $ age) = explode ("|", $ line); // use explode to separate | as a separator and divide it into three parts, then, use the "list" command to separate the three variables into the three variables} echo $ name; echo $ sex; echo $ age;
Use range to generate an array
$ Test = range (); // generates an array of 6 elements ranging from 1 to 6. $ test = range (, 2); // The third parameter is the step size, the step size is probably the meaning of the interval. here 1 3 5... is generated .... Value with an interval of 2
Test array

Use is_array for testing. true and false are returned.

Print array

Print with print_r

Print_r ($ states); // Print the array through traversal

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.