Introduction to PHP arrays

Source: Internet
Author: User
Tags string format

I. Classification of PHP arrays

The arrays in PHP are divided into associative arrays and indexed arrays, depending on the subscript:
Indexed array: The subscript starts at 0 and grows sequentially.

$arr =[1,2,3,4,5];

Associative arrays: The subscript is a string format, and each subscript string corresponds to the value one by one of the array. (a bit like the object's key-value pair)

1 $arr =["a" =>1; " B "=>2;" C "=>3];

Note: The [] declaration is not available until the 5.4 release.

Two. About associative arrays and indexed arrays

1. In an array, an indexed array and an associative array can exist at the same time

Array (=>4, "four");

2, in the array, all the indexed array, if not specified, will be stripped of the associated items, the default growth (that is, the associative array does not occupy the index bit)

Array (=>4,5,6, "four");

The 1,2,3,5,6 index is 0 1 2 3 4, respectively.
3. If the key of the associative array is a pure decimal integer string, the number is converted to the index value of the indexed array

Array (=>4, "9");

1 2 3 4 The index is 0 1 2 9 respectively.
4. If you manually specify the key of the associative array, the subscript of the indexed array, and if it is repeated with the previous key or subscript, then the value specified will overwrite the previous value

Array ("One" =>5, "one" =>6)

Prints an array of 1 2 "one" =>6.
5, if the index array is manually specified subscript, then the subsequent self-growth subscript, according to the previous subscript maximum, followed by growth

Array (=>4,5, "9");

1 2 3 4 5 The index is 0 1 2 9 10 respectively.

Three. Iterating the array

1. Iterating through the array with a For loop
Count ($arr), used to count the number of array elements
A For loop can only be used to traverse a purely indexed array
If there is an associative array, count counts the total number of the two arrays, and using a for loop to iterate through the mixed array will cause the array to go out of bounds!

$arr =[1,2,3,4,5];for ($i =0; $i <count ($arr); $i + +) {echo $arr [$i];}

2. The Foreach loop iterates through the array (the most common method)
foreach can iterate over any type of array, as in the following case:

$arr =[1,2, "One" =>5, "a" =>6];foreach ($arr as $key + $value) {echo "This is key:{$key}"; echo "This is value:{$value}";}

3. Using list () each () while iterating over an array

List (); Used to assign each value of an array to each parameter of the list function. (The parameter of the list function must be less than the number of elements equal to the array)

Attention:

List () When parsing an array, only the indexed array is parsed and the associated item cannot be fetched.

The list () can optionally parse the value of an array by an empty parameter;

each (); Returns the key-value pair in which the current pointer is in the array, and moves the pointer back one bit.

Return value: an array. Contains an indexed array (0 key 1 values) and an associative array ("Key" key "value").

while (list ($key, $value) = each ($arr)) {echo ' {$key}&nbsp;{ $value}<br> ";} Reset ($arr);

Note: After iterating through the array with each, the pointer is always the next bit in the last digit, that is, use each again, always return false, and if necessary, reset the array pointer with the reset () function.

4. Iterating through arrays using array pointers

Next (): Moves the array pointer back one bit and returns the value of the next bit without returning false.

Prev (): Moves the array pointer forward one bit and returns the value of the previous bit without returning false.

End (): Moves the pointer of the array directly to the last one, and returns the value of the last one, and the empty array returns false.

Reset (): Resets the array pointer back to the first bit and returns the value of the first bit, and the empty array returns false.

Key (): Returns the key that the current pointer is in, and returns null if the array is empty or the pointer is to the last point.

Current (): Returns the value that is currently in place of the pointer, or FALSE if the array is empty or the pointer is to the last.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

$arr =[1,2,false,3,4,5];while (True) {echo key ($arr);    echo "---"; Echo current ($arr). "    <br> ";            if (!next ($arr) &&key ($arr) ===null) break; }}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>


Introduction to PHP arrays

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.