A more comprehensive method of using PHP arrays summary _php Tips

Source: Internet
Author: User
Tags numeric mixed php database
One, what is an array
An array is a set of data that organizes a series of data into an operable whole. Each entity of an array contains two items: The key and the value.
Ii. Declaration of data
There are two main ways to declare an array in PHP:
One is To declare an array by applying the array () function,
One is assign a value directly to an array element。 Fei Fei asp! Technology Park
The <1>array () function declares an array of arrays ([mixed ...]) parameter mixed syntax is Key=>value
Such as

<?php
$array =array ("1" => "knitting", "2" => "Cheng", "3" => "word", "4" => "canon");
Print_r ($array);
echo "<br>";
echo $array [1]; Note: The subscript defaults to start at 0
Echo $array [2];
Echo $array [3];
Echo $array [4];
?>

<2> assigns values directly to an array element.
This is a good way to create an array if you don't know the size of the array you created, or if the size of the array might change when you actually write the program.
Such as

<?php
$array [1]= "I";
$array [2]= "Love";
$array [3]= "PHP";
Print_r ($array); Output the structure of the array created
?>

iii. type of array
PHP supports two types of arrays:indexed Array (indexed array)AndUnion Array (associative array),The former uses a number as a key, and the latter uses a string as the key.
four, output array
In PHP, the array element output can beEchoAndPrintStatement, but this can only be output to an element in the array;Print_r ()function, whose syntax:Print_r (mixed expression),Parameter expression is a normal shape, character, or real variable that outputs the variable itself, and if the argument is an array, all the elements in the array are displayed in the order of the 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.Fei Fei AS.P Technology Park
Declares an array: A type descriptor array name [constant expression];
Two-dimensional arrays:
When an array is an array of elements, it is called a two-dimensional array.
Such as

<?php
$str = Array (
"Network programming language" =>array ("PHP", "JSP", "ASP"),
"Sports Project" =>array ("M" => "soccer", "N" => "basketball"));
Print_r ($STR);
?>

Six, traversing the array
Traversing all the elements in an array is a commonly used operation that can complete queries or other functions during traversal. There are a number of ways to traverse an array in PHP, and the two most commonly used methods are described below.
<1> traversing arrays using a foreach structure;
<2> use the list () function to traverse an array, and the list () function can only be used for arrays of numeric indices, and the numeric index starts at 0.
Example: Comprehensive use of List () and each () authenticated user login:

<?php
Output User Login information
while (list ($name, $value) =each ($_post)) {
if ($name!= "Submit") {
echo "$name = $value <br>";
}
}
?>

statistics of the number of array elements
In PHP, use thecount ()The number of elements in the array of functions is counted, and the syntax is: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 will recursively array the arrays. Such as

<?php
$array = Array ("PHP" => Array ("PHP function Reference encyclopedia", "PHP Program Development Example Treasure", "PHP Database system Development Complete manual"),
"ASP" => Array ("ASP experience Skill Treasure")
); Declare a two-dimensional array
echo Count ($array, count_recursive);//recursive statistics the number of array elements, running with a result of 6
?>

Eight, array sorting
<1> Use Sort () and rsort () to perform ascending and descending order of the array, for example,
<?php
$array =array (5,26,37,18,9,42,88,66);
$array 1=sort ($array);
for ($i =0; $i <count ($array); $i + +) {
echo $array [$i]. " ";
}
echo "<br>";
$array 1=rsort ($array);
for ($i =0; $i <count ($array); $i + +) {
echo $array [$i]. " ";
}
?>
Run Result:
5 9 18 26 37 42 66 88
88 66 42 37 26 18 9 5
<2> sort associative arrays using ksort () and Asort ()
If you use an associated array, you want to keep the sort of keyword and value sorted after sorting, which requires the use of the 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);
?>
Run 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.