PHP Array Operations

Source: Internet
Author: User

PHP array 1. Numeric index array 1.1 create an array

The two most commonly used types in PHP are: arrays, strings. There are two types of arrays, one is a numeric index, one is the key is the index, and the keyword index is a bit like the meaning of a dictionary in Python.
How to create an array of numeric index types

$products Array (' Tires ', ' oil ', ' Spark plugs ');

Even if an array does not exist, it is possible to assign operations directly to an array, which naturally produces a set of values, such as the following:

$products [0] = ' Tires '; $products [1] = ' oil '; $products [2] = ' Spark plugs ';
1.2 Accessing array elements

Use subscript to index when accessing.

Echo $products [0];
1.3 Using a loop to access an array

The iterate array has for and foreach, one for the numeric index array and one for the key word Group index

     for ($i$i$i+ +)       {echo$products[$i];    }
2. Non-numeric indexed array 2.1 Create an array

Create a keyword index array

$prices Array (' Tires ' =>100, ' oil ' =>10, ' Spark Plug ' =>4);

With the following, which is also equivalent

$prices [' Tires '] = +; $prices [' oil '] = ten; $prices [' Spark plugs '] = 4;

2.2 Accessing array elements

When you visit, change the digital subscript to a keyword.

Echo $prices [' Tires '];
2.3 Using a loop to access an array

There are three ways to use loops to handle key word groups. foreach or list () or each ()
First Look at foreach:

foreach ($pricesas$key$value) {    echo$key. " -". $value. " <br/> ";} foreach ($pricesas$value) {    echo$value} 

In the first foreach, the elements in the array $prices are separated by a group, and the keys and values in the beauty group are assigned to $key and $value respectively, and the second is to assign the value to the variable $value by default.
Look at each ():

 while ($elementeach ($prices)) {    echo$element[' key ']. -‘. $element [' Value ']. " <br/> ";}

Each () is passed to $element pair of key-value pairs each time, and then $element becomes an array containing a key element and a value element. each () and while
Last look at List ():

 while (list($product,$priceeach ($prices)) {    echo ] $product $price<br/> ";}

List () only assigns a pair of key-value pairs passed in each () to list ().

3. Array operators

Array has only one union (+) operator
$a + $b: The array $b is appended to $ A, but any element of the same keyword will not be added, quite a collection, and the collection is not allowed to be duplicated.

4. Multidimensional arrays

A two-dimensional array is a two-bit table. As an example of a two-bit array, the others are similar:

$products Array (    array(' TIR ', ' Tires ', '), array (' oil ', ' oil ', '    ),    array(' SPK ' , ' Spark plugs ', 4))

The Calling method is:
$products[0][1];
One way to use a For loop is to:

 for ($row= 0; $row<3; $row+ +)    {for ($col=0;  $col<3; $col+ +)        {echo$products[$row] [$col];    }     Echo "<br/>";}

The method of a multidimensional array of keywords is similar.

5. Array sorting 5.1 Three sorting functions sort (), Asort (), Ksort ()

The sort () function sorts the array of numeric indices. Arranged in ascending alphabetical order, uppercase letters in front of lowercase letters, receive parameters as an array, and an optional parameter, the value of this parameter is: sort_regular (default), sort_numeric,sort_string. The function is sorted by number and character order.
sourt($array);
Asort () and Ksort () are sorted by value and by keyword for the key word group, respectively.
Asort () is the ordering of the values of each element, and Ksort () is the ordering of the element's keywords.

5.2 Reverse Sort

Reverse ordering is to reverse the order of the previous three functions, from the function name, simply add an R to the function name.
sort (), Asort (), Ksort ( ) correspond to the reverse order:Rsort (), Arsort (), Krsort ().

The ordering of multidimensional arrays requires that you set the comparison function yourself.

6. Array functions

The functions that are commonly used in arrays are:

6.1 Navigating through the array

Each array has an internal pointer to which of the elements is currently in use.
Each () returns the elements in the array in turn.
Current () Returns an element that is pointed to by an array pointer, for example, when a loop is performed on an array, and a break occurs after three elements are called, the current pointer points to the 4th element, at which point Then the fourth element of the array is returned.
Reset () returns the first element of the array.
End () returns the last element of the array.

6.2 Manipulating each element in an array

Array_walk ($array, FUNC,AGRV) modifies each element in the array in the same way.
Receive two parameters, and the third is optional:
The first parameter is the array to manipulate, the second argument is the method of operation, a function that can be customized, and the third argument is the parameter that the function needs to pass in.

6.3 Count Array elements

The function of the count () and sizeof () functions is the same as the use method, which is the number of elements in the statistical array. No difference.
array_count_values () This function counts the number of occurrences of each particular value in the array.

PHP Array Operations

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.