Basic methods of operation for PHP arrays

Source: Internet
Author: User
Tags sorts

1. Basic functions of array manipulation

The key and value of the array:
Array_values ($arr); Get the value of an array
Array_keys ($arr); Gets the key name of the array
Array_flip ($arr); the values in the array are exchanged with the key names (if there are duplicates, the previous one will be overwritten by the following)
In_array ("Apple", $arr); Retrieve Apple in an array
Array_search ("Apple", $arr); Retrieve Apple in an array if there is a return key name
Array_key_exists ("Apple", $arr); Retrieves whether the given key name exists in the array
Isset ($arr [Apple]): Retrieves whether the given key name exists in the array

Conversions between arrays and variables:
Extract ($arr); used to convert an element in an array into a variable and import it into the current file, the key name as the variable name, and the value as the variable value
Note: (The second parameter is very important, can see the manual use) method of use echo $a;
Compact (VAR1,VAR2,VAR3); Create an array with the given variable name

Internal pointers to arrays:
Current ($arr); Returns the cell in the array
POS ($arr); Returns the current cell in the array
Key ($arr); Returns the key name of the current cell in the array
Prev ($arr); Rewind the internal pointer in the array back to a
Next ($arr); Move the inner pointer in the array forward one
End ($arr); point the inner pointer in the array to the last cell
Reset ($arr; point the inner pointer in the array to the first cell
Each ($arr), constructs an array that returns a key name/value of the current element of the array, and moves the array pointer forward one
List ($key, $value) =each ($arr); Gets the key name and value of the current element of the array

2. Sorting of arrays

Sort by array of element values:
Sort ($arr), sorted by order of small to large (the second argument is sorted by what sort), and the array ordering of the key names is ignored
Rsort ($arr); Sort by order of large to small (the second argument is sorted by what sort) ignores the array ordering of the key names
Usort ($arr, "function"); Sorts the values in the array using a user-defined comparison function (there are two parameters in function, 0 for equality, positive for the first is greater than the second, and negative for the first less than the second) to sort the array that ignores the key name:
Asort ($arr); Sort by the order of small to large (the second argument is sorted by what way) the array of reserved key names
Arsort ($arr); Sort by order of large to small (the second parameter is sorted by what sort) the array of reserved key names
Uasort ($arr, "function"); Sorts the values in the array using a user-defined comparison function (there are two parameters in function, 0 equals, positive numbers indicate that the first is greater than the second, and negative numbers represent the first one less than the second) to sort the array of reserved key names.

Sort arrays by key names
Ksort ($arr); Order by Key Name
Krsort ($arr); reverse order by Key name
Uksort ($arr, "function"); Sorts the key names in the array using a user-defined comparison function (two parameters in function, 0 for equality, positive for first greater than second, negative for first less than 2nd)

Sort by natural sort
Natsort ($arr); natural sort (Ignore key name)
Natcasesort ($arr); natural sort (ignoring case, ignoring key name)

Array sorting:

Use the sort () function:
The sort () function is letter-case-sensitive. All the letters are in front of the lowercase letters. So ' a ' is less than ' Z ', and ' Z ' is less than ' a '.
The second parameter of the function is optional. This optional parameter can be passed Sort_regular (default), Sort_numeric, or sort_string. The ability to specify sort types is useful, for example, when you want to compare strings that may contain numbers 2 and 12. From a mathematical point of view, 2 is less than 12, but as a string, ' 12 ' is less than ' 2 '.

Use the Asort () function and the Ksort () function to sort the related array:
The function Asort () is sorted according to the value of each element of the array. The Ksort () function is sorted by keyword rather than by value.

Reverse Sort:

The function rsort () sorts a one-dimensional numeric index array in descending order. The function arsort () sorts a one-dimensional correlation array in descending order of the value of each element. The function krsort () pin sorts a one-dimensional array in descending order based on the keyword of the array element.
To access data in a one-dimensional array, you need to use the name of the array and the index of the element, except that an element has two indexes--the two-dimensional array and the one-dimensional array are similar, in addition to the rows and columns.
You can use the dual for loop to achieve the same effect:
for ($row =0; $row <3; $row + +)
{
for ($column =0; $column <3; $column + +)
{
echo ' | '. $products [$row] [$column];
|
echo ' |<br/> ';
}
If you use this code for a large array, it will be much simpler.
You might prefer to create a column name instead of a number. You can use the following code:
$products = Array (' Code ' = ' TIR ', ' descrīption ' = ' Tires ', ' Price ' =>100), Array (' code ' "= ' oil ', ' Des Crīption ' = ' oil ', ' price ' =>10), array (' Code ' = ' SPK ', ' descrīption ' = ' Spark plugs ', ' Price ' =>4)};
It is much easier to use this array if you want to retrieve a single value. Keep in mind that storing the described content in a column named after its name is easier to remember than saving it in the so-called first column. With descriptive indexes, it is not necessary to remember that an element is stored in the [x][y] position. Using the name of a pair of meaningful rows and columns as an index makes it easy to find the data you need.
Then we can't use a simple for loop to iterate through each column sequentially. You can use the For loop to iterate through an external numeric index array, $products. Each row of the $products array is an array with a descriptive index. Use the each () and list () functions in the while loop to traverse the entire internal array. Therefore, you need a for loop with a while loop embedded in it.
for ($row = 0; $row < 3; $row + +}
{
while (list ($key, $value) = each ($products [$row])
{
echo "| $value";
}
echo ' |<br/> ';
}
Three-dimensional arrays have high, wide, and deep concepts. If you can easily imagine a two-dimensional array as a table with rows and columns, you can think of a three-dimensional array as a bunch of tables like this. Each element can be referenced by layers, rows, and columns.
Depending on how you create a multidimensional array, you can create four-dimensional, five-, or six-dimensional arrays. In PHP, there is no limit to the number of array dimensions, but it is difficult to imagine an array that is more than three-dimensional. Most of the practical problems logically require only three-dimensional or less-dimensional array structures to be used.

Basic methods of operation for 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.