A detailed explanation of the PHP array

Source: Internet
Author: User

Definition of an array

The essence of an array is to manage and manipulate a set of variables, which can store arbitrary lengths of data, and can store any type of data. The cells in the array are called elements, each element includes subscripts (keys) and values, and when the element is accessed, it is accessed by subscripts, including one-dimensional arrays, two-dimensional arrays, and multidimensional arrays (that is, arrays nested), and PHP is divided into indexed arrays and associative groups.

(1) Indexed arrays: Use integers as indexes, such as $arr=array (' PHP course ', ' HTML course ', ' CSS Course ');

(2) Associative arrays: use strings as indexes, such as $arr=array (' ID ' =>1, ' name ' = ' php course ', ' class=> ' PHP100 ');

Declaration and use of PHP arrays

1. Assigning a direct array element to a value declaration

If index subscript is not given, it will be indexed sequentially from 0, and if index subscript is given, the next one will increment by 1 from the largest subscript, and if the preceding subscript appears, the previous element will be re-assigned, and the indexed array and associative array do not affect each other when a mixed declaration occurs.

For example:

$array [0]= "I"; $array [1]= "Love"; $array [2]= "PHP";p Rint_r ($array);

where Print_r () is a special function that allows you to view the values inside a PHP array variable, displaying all the elements in the array in the order of key values and elements. This is useful for debugging a program.

2. Using the array () function declaration

The default is an indexed array, which, if an associative array, requires an array to be assigned subscripts, using "key = = value", and "," splitting between multiple members.

For example:

$fruits = Array (' red ' = ' apple ', ' yellow ' = ' banana ', ' purple ' = ' plum ', ' green ' = ' grape '); Print_r ($fruits);

The traversal of the PHP array

We often want to traverse the array, PHP in the way to iterate over the array, you can use the for () loop to iterate over the arrays, here, often use the sizeof () function, which is one of the commonly used array functions, returns the size of the array, that is, reading the number of elements within the array, as the maximum value of the loop counter You can also use the list () function to iterate through an array, which can only be used for arrays of numeric indexes, and the numeric index starts at 0.

PHP can also be used specifically for Array loop purpose functions: foreach (). foreach () executes once for each element in the array passed to it, and it does not require a counter or a call to the function sizeof (), which automatically tracks the position of the array in the array while requiring less maintenance. foreach () has two syntactic constructs:

(1) foreach (array_expression as $value) (2) foreach (array_expression as $key = $value)

The first structure iterates over the given array_expression array, each time the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step. In the second structure, the key name of the current cell is also assigned to $key in each loop.

The Foreach loop runs to the end, and the inner pointer of the original array points to the end of the array. For example:

foreach ($arr as $value) {    echo "value: $value";} foreach ($arr as $key = $value) {    echo "key: $key; Value: $value ";}

Ordering of PHP arrays

The array elements are sorted, we use more when we do the project, there are a lot of related functions involved, such as sort (), Rsort (), Usort (), Ksort (), Uasort (), Uksort (), and so on, here are a few. Use sort () and rsort () to sort the arrays in ascending and descending order, for example:

$arr =array (23,4,65,11,64,8), sort ($arr);p rint_r ($arr);

Operation Result:

Array ([0] = 4 [1] = 8 [2] = = [3] = [4] = [5] = 65)

In addition, we can note that after sorting by the sort function, the original index key name of the array is reassigned. Rsort () Reverses the order of the arrays.

If an associative array is used, the ordering of the keywords and values remains consistent after sorting, which requires the use of the Ksort () and Asort () functions, for example:

$array =array (' php ' =>1, ' jsp ' =>2, ' ASP ' =>3); Ksort ($array);p rint_r ($array);

Operation Result:

Hopefully the above can help you better understand and use PHP arrays.

    • This article is from: Linux Learning Tutorial Network

A detailed explanation of the PHP array

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.