PHP array usage [comprehensive method summary]

Source: Internet
Author: User
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. 1. what is an array is a set of data, which organizes a series of data to form an operable whole. Each object in the array contains two items: Key and value. 2. declare data in PHP "> <LINKhref =" http://www.php100.com/

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.
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,

$ Array = array ("1" => "le", "2" => "Cheng", "3" => "word ", "4" => "Dian ");
Print_r ($ array );
Echo"
";
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,

$ 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,

$ 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:

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

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,

$ Array = array ("php" => array ("PHP Function Reference", "PHP program development example", "PHP database system development full manual "),
"Asp" => 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,
$ Array = array );
$ Array1 = sort ($ array );
For ($ I = 0; $ I Echo $ array [$ I]. "";
}
Echo"
";
$ Array1 = rsort ($ array );
For ($ I = 0; $ 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,
$ Array = array ('php' => 1, 'JSP '=> 2, 'asp' => 3 );
Ksort ($ array );
Print_r ($ array );
Echo"
";
Asort ($ array );
Print_r ($ array );
?>
Running result:
Array ([asp] => 3 [jsp] => 2 [php] => 1)
Array ([php] => 1 [jsp] => 2 [asp] => 3)

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.