Summary of the PHP array (very comprehensive AH)

Source: Internet
Author: User
Tags array sort php database

First, what is an array
An array is a set of data that organizes a series of data to form an operational whole. Each entity of an array contains two items: A key and a value.

II. Statement of data
There are two main ways to declare an array in PHP: One is to apply an array () function declaration, and one is to assign a value directly to the element.
The <1>array () function declares an array of ways in which the syntax of the [mixed] parameter mixed is Key=>value
For example, <?php
$array =array ("1" = "series", "2" = "Cheng", "3" = "CI", "4" and "Code");
Print_r ($array);
echo "<br>";
echo $array [1]; Note: The subscript default is starting from 0
Echo $array [2];
Echo $array [3];
Echo $array [4];
?>
<2> assigns a value directly to an array element. If you do not know the size of the array created when you create the array, or if the size of the array may change when you actually write the program, the method used to create this array is preferable.
For example, <?php
$array [1]= "I";
$array [2]= "Love";
$array [3]= "PHP";
Print_r ($array); Output the structure of the created array
?>

Iii. Types of arrays
PHP supports two arrays: an indexed array (indexed array) and a union array (associative array), which uses a number as the key, which uses a string as the key.

Iv. Output Arrays
The output of an array element in PHP can be achieved through ECHO and print statements, but this can only be output from an element in an array, and the Print_r () function is used to output the structure of the arrays, with the syntax: Print_r (mixed expression), The parameter expression is a normal shape, character, or real variable that outputs the variable itself, and if the parameter is an array, all the elements in the array are displayed in the order of key values and elements.

V. Construction of arrays
One-dimensional arrays:
When an element of an array is a variable, it is called a one-dimensional array.
Declares an array: type specifier array name [constant expression];
Two-dimensional arrays:
When an element of an array 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);
?>

Six, iterating through an array
is a common operation that iterates through all the elements in an array, and can perform queries or other functions during traversal. There are several ways to iterate through an array in PHP, and the two most commonly used methods are described below.
<1> iterate through the array using the foreach structure;
<2> use the list () function to iterate through an array, the list () function can only be used for arrays of numeric indexes, and the numeric index starts at 0.
Example: Comprehensive use list () and each () authenticated user login:
<?php
//Output user logon information
while (list ($name, $value) =each ($_post)) {
if ($ Name!= "Submit") {
   echo "$name = $value <br>";
}
}
?
Vii. Statistics array elements
in PHP, the count () function counts the number of elements in a group, with the syntax: Int coun (mixed array[,int mode]), Where the parameter array is the necessary parameter, mode is an optional parameter, and if Count--recursive (or 1) is selected, this function recursively sets an array of arrays. For example,
<?php
$array = Array ("php" + = Array ("PHP function Reference", "PHP Program Development Sample Treasure", "PHP Database system Development Complete Manual"),
                "asp" = = Array ("ASP experience Tips")
         ); Declares a two-dimensional array         
Echo Count ($array, count_recursive);//The number of elements of the recursive statistic array, The result of the operation is 6
.

Eight, array sort
<1> use sort () and rsort () to order ascending and descending pairs of arrays, such as
<?php
$array =array (5,26,37,18,9,42,88,66);
$array 1=sort ($array);         
for ($i =0; $i <count ($array); $i + +) { &NBSP;&NBSP;&NBSP,
   echo $array [$i]. " &nbsp;&nbsp; "; &NBSP;&NBSP;&NBSP
}
Echo <br>  
$array 1=rsort ($array);        
for ($i =0; $i <count ($array); $i + +) {      
echo $array [$i]. " &NBSP;&NBSP ";
}
?
Run Result:
5 9 88 
9 5   
<2> using Ksort () and the AS ORT () sort the associative array
If you use a related array, you also keep the keyword and value sorted after sorting, which is required to use Ksort () and Asort () letters

Number, such as,
<?php
$array =array (' php ' =>1, ' jsp ' =>2, ' ASP ' =>3);
Ksort ($array);
Print_r ($array);
echo "<br>";
Asort ($array);
Print_r ($array);
?>
Operation Result:
Array ([ASP] = 3 [JSP] = 2 [PHP] + 1)
Array ([PHP] = 1 [jsp] = 2 [ASP] + 3)

(go) Summary of PHP arrays (very comprehensive AH)

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.