Php array usage

Source: Internet
Author: User
Tags php database

1. What is an array?
An array is a collection of data. It organizes a series of data to form an operable whole. Each object in the array contains two items: Key and value.
Ii. Declare data
There are two ways to declare arrays in PHP:
First, apply the array () function declaration,
First, assign values directly to array elements. Flying Asp! Technology Park
<1> array () function declaration array method array ([mixed...]) the syntax of the parameter mixed is key => value
For example,

<? Php tutorial
$ Array = array ("1" => "Le", "2" => "Cheng", "3" => "Word ", "4" => "Dian ");
Print_r ($ array );
Echo "<br> ";
Echo $ array [1]; // Note: The subscript starts from 0 by default.
Echo $ array [2];
Echo $ array [3];
Echo $ array [4];
?>

<2> assign values directly to array elements.
If you do not know the size of the created array when creating the array, or the size of the group may change when writing the program, this method is better.
For example,

<? Php
$ Array [1] = "I ";
$ Array [2] = "love ";
$ Array [3] = "PHP ";
Print_r ($ array); // outputs the structure of the created array
?>

3. array type
PHP supports two types of Arrays: indexed array and associative array. The former uses numbers as keys, and the latter uses strings as keys.
Iv. Output Array
The array element output in PHP can be achieved through the echo and print statements, but this can only be achieved for an element in the array. to output the array structure, you must use the print_r () function, syntax: print_r (mixed expression). If the expression parameter is a common integer, positive, or real variable, the variable itself is output. If the parameter is an array, all elements in the array are displayed in the order of key values and elements.
5. Array Construction
One-dimensional array:
When an array element is a variable, it is called a one-dimensional array. Apsara As. p Technology Park
Declare an array: array name of the type specifier [constant expression];
Two-dimensional array:
When an array element is an array, it is called a two-dimensional array.
For example,

<? Php
$ Str = array (
"Network Programming Language" => array ("PHP", "JSP", "ASP "),
"Sports" => array ("m" => "football", "n" => "basketball "));
Print_r ($ str );
?>

6. Traverse Arrays
Traversing all elements in an array is a common operation. You can perform queries or other functions during traversal. There are multiple methods to traverse arrays in PHP. The following describes the two most commonly used methods.
<1> use the foreach structure to traverse the array;
<2> use the list () function to traverse the array. The list () function can only be used for numeric index arrays, And the numeric index starts from 0.
For example, use list () and each () to authenticate user logon:

<? Php
// Output user login information
While (list ($ name, $ value) = each ($ _ POST )){
If ($ name! = "Submit "){
Echo "$ name = $ value <br> ";
}
}
?>

7. count the number of array elements
In PHP, the count () function is used to calculate the number of elements in the array. Syntax: int coun (mixed array [, int mode]). The parameter array is a required parameter, mode is an optional parameter. If COUNT--RECURSIVE (OR 1) is selected, this function recursively applies the array. For example,

<? Php
$ Array = array ("php" => array ("PHP function reference", "PHP program development example", "PHP database tutorial system development full manual "),
"Asp tutorial" => array ("ASP lessons and skills ")
); // Declare a two-dimensional array
Echo count ($ array, COUNT_RECURSIVE); // recursively count the number of array elements. The running result is 6.
?>

8. array sorting

<1> Use sort () and rsort () to sort arrays in ascending or descending order, for example,
<? Php
$ Array = array );
$ Array1 = sort ($ array );
For ($ I = 0; $ I <count ($ array); $ I ++ ){
Echo $ array [$ I]. "";
}
Echo "<br> ";
$ Array1 = rsort ($ array );
For ($ I = 0; $ I <count ($ array); $ I ++ ){
Echo $ array [$ I]. "";
}
?>

Running result:
5 9 18 26 37 42 66 88
88 66 42 37 26 18 9 5
<2> sort the joined Array Using ksort () and asort ()
If the related array is used, the sorting of keywords and values must be consistent after sorting. This requires the ksort () and asort () functions.
Number, for example,

<? Php
$ Array = array ('php' => 1, 'jsp example' => 2, 'asp '=> 3 );
Ksort ($ array );
Print_r ($ array );
Echo "<br> ";
Asort ($ array );
Print_r ($ array );
?>
Running result:
Array ([asp] => 3 [jsp] => 2 [php] => 1)
Array ([php] => 1 [jsp] => 2 [asp] => 3)


Array ("key" => "value"); creates an array

// Display an array

Print_r ($ array );

// Use the compact () function to create an array and use parameters as the unit of the new array;

$ NewArray = compact ("red", "green", "yellow", "blue", "array ");

// Use the extract () function to convert the elements in the array into variables.

Extract ($ exArray );

Echo "$ key1 $ key2 $ key3 $ key4 $ key5 ";

※Check value and key

Array_key_exists ($ key, $ array); // check the array key

In_array ($ value, $ array); // check the value in the array

※Get Value

// Use array_values () to obtain the array value

$ CarValues = array_values ($ car );

// Retrieve the key name of the array

$ TwoKeys = array_keys ($ two );

Key ($ array); // key name of the current output unit

// After the array is defined, use current () to obtain the value of the current unit.

$ Red = current ($ array );

List ($ red, $ green) = $ array; // assign values in the array to variables, $ array = array ("red", "green ");

Each ($ two); // returns the key and value of the current cell in the array.

※Traversing an array

Foreach ($ two as $ subArray); // traverses the Array

While (list ($ key, $ value) = each ($ array )){

Echo "$ key => $ value,"; // use each to traverse the Array

}

※Fill an array

// Fill in the array to the left and right

Array_pad ($ array, + 3, "shuzhi"); // The value 2 is filled from left to right. It is filled when the value is greater than the number of units.

$ Array1 = array_fill (5, 5, "test"); // use array_fill () to fill in the value of this array. The value is test, which is filled from 5th units. A total of five units are filled.

// Fill in the array key name

$ Keys = array ('string', 5, 10, 'str ');

$ Array3 = array_fill_keys ($ keys, "array value ");

// Use the array_filp () function to exchange key names and values

$ Speed = array_flip ($ speed );

// Use the array_splice () function to replace the value of the first unit with 7

$ Output = array_splice ($ input, 6, 0, 7 );

// Use the array_splice () function to delete the array unit. Only the first five units are retained.

$ Output = array_splice ($ input, 5 );

$ Array1 = range (10,100, 10); // use the third parameter of the range () function to set the step value between units.

※Sort

Shuffle ($ array); // disrupt the array order

// Use array_multisort () to sort the three Arrays

Array_multisort ($ sort1, $ sort2, $ sort3 );

// Sort the array and maintain the index relationship.

Asort ($ array );

// Sort the test array in reverse order and maintain the index relationship

Arsort ($ array );

// Sort the array key names using ksort ()

Ksort ($ array );

// Use the krsort () function to sort the keys in reverse order.

Krsort ($ array );

// Sort the test Array Using sort () [sort by key name]

Sort ($ array );

// Use natsort () to sort [natural sorting, in numerical order] case sensitive to unit values

Natsort ($ array );

// Use the natcasesort () function to sort [natural sorting] but ignore the case sensitivity of the value

Natcasesort ($ array );

// Use the array_reverse () function to sort the array units in reverse order.

$ NewArray = array_reverse ($ array, TRUE); // retain the original key name when set to TRUE

※Intersection and difference set

// Use array_diff () to calculate the difference set of the three arrays [Comparison of array values]

$ Result = array_diff ($ dog1, $ dog2, $ dog3 );

// Use array_diff_assoc () to calculate the difference set of the three arrays [Comparison of values and key names]

$ Result = array_diff_assoc ($ dog1, $ dog2, $ dog3 );

// Use array_diff_key () to calculate the difference set of the three arrays [comparison key name]

$ Result = array_diff_key ($ dog1, $ dog2, $ dog3 );

// Use array_intersect () to calculate the intersection of the three arrays [Comparison of array values]

$ Result = array_intersect ($ dog1, $ dog2, $ dog3 );

// Use array_intersect_assoc () to calculate the intersection of the three arrays [Comparison of values and key names]

$ Result = array_intersect_assoc ($ dog1, $ dog2, $ dog3 );

// Use array_intersect_key () to calculate the intersection of the three arrays [comparison key name]

$ Result = array_intersect_key ($ dog1, $ dog2, $ dog3 );

※Merge Arrays

// Use the array_merge () function to merge Arrays

$ Result = array_merge ($ array1, $ array2, $ array3, $ array4, $ array5 );

Array_rand ($ input, 10); // randomly retrieve 10 units

Count ($ array, COUNT_RECURSIVE); // display the number of array units. Parameter 2 can only be 1 or COUNT_RECURSIVE, and sometimes can traverse multi-dimensional arrays.

※Warehouse receiving/picking Stack

// Array output stack. The last unit of the array is displayed.

Array_pop ($ array );

// Add two values, 7 and 8, to the end of the array

Array_push ($ array, 7,8 );

// Remove the elements starting with the array from the array

Array_shift ($ array );

// Add 7, 8 to the beginning of the array

Array_unshift ($ array, 7,8 );

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.