How to use PHP Arrays _php tutorial

Source: Internet
Author: User
Tags compact 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:
The first is to apply an array () function declaration,
One is to assign a value directly to the array element. Fei Fei asp! Technology Park
The <1>array () function declares an array of ways in which the syntax of the [mixed] parameter mixed is Key=>value
Such as

$array =array ("1" = "series", "2" = "Cheng", "3" = "CI", "4" and "Code");
Print_r ($array);
echo "
";
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.
Such as

$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. Fei Fei AS.P Technology Park
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.
Such as

$str = Array (
"Network programming language" =>array ("PHP", "JSP", "ASP"),
"Sports" =>array ("M" = "Football", "n" = "basketball");
Print_r ($STR);
?>

Vi. iterating through an array
Iterating through all the elements in an array is a common operation that can be done during the traversal of a query or other function. There are several ways to iterate through an array in PHP, and the two most commonly used methods are described below.
<1> iterating through an array using the foreach structure;
<2> using 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:

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

Vii. number of elements in the statistics array
In PHP, use the count () function to count the number of elements in the array, with the syntax: Int coun (mixed array[,int mode]), where the parameter array is the necessary parameter, and mode is an optional parameter, if the Count--recursive is selected ( or 1), this function recursively sets an array of arrays. Such as

$array = Array ("php" = = Array ("PHP function Reference", "PHP Program Development Sample Treasure", "PHP Database Tutorial System Development Complete Manual"),
"ASP Tutorial" = = Array ("ASP experience Tips")
); Declaring a two-dimensional array
echo Count ($array, count_recursive);//The number of elements of the recursive statistic array, with a run result of 6
?>

Eight, array sorting

<1> use Sort () and rsort () to order ascending and descending pairs of arrays, for example,
$array =array (5,26,37,18,9,42,88,66);
$array 1=sort ($array);
for ($i =0; $i
echo $array [$i]. " ";
}
echo "
";
$array 1=rsort ($array);
for ($i =0; $i
echo $array [$i]. " ";
}
?>

Operation Result:
5 9 18 26 37 42 66 88
88 66 42 37 26 18 9 5
<2> sorting associative arrays using Ksort () and Asort ()
If a related array is used, the ordering of the keywords and values remains consistent after sorting, which is required using the Ksort () and Asort () letters
Number, such as,

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


Array ("Key" = "value");

Display array

Print_r ($array);

Use the compact () function to create a new array and take the parameters as a unit of the new array;

$newArray = Compact ("Red", "green", "yellow", "Blue", "array");

Use the Extract () function to convert a cell in an array to a variable

Extract ($exArray);

echo "$key 1 $key 2 $key 3 $key 4 $key 5";

※ Check value, key

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

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

※ Get Value

Use Array_values () to get the value of the array

$carValues = Array_values ($car);

Remove the key name of the array

$twoKeys = Array_keys ($two);

Key ($array);//Output The key name of the current cell

After the array is defined, use current () to get the value of the cell

$red = current ($array);

List ($red, $green) = $array;//assigns the value in the array to the variable, $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);//traversal array

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

echo "$key = = $value,";//use each to iterate through an array

}

※ Fill Array

padding arrays left and right

Array_pad ($array, +3, "Shuzhi"),//2 parameter is filled from left to right, number is greater than number of cells

$array 1 = Array_fill (5,5, "test");//Use Array_fill () to populate the value of this array with a value of test, starting with the 5th cell and filling 5 cells altogether

Populate array key names

$keys = Array (' String ', 5, ' str ');

$array 3 = Array_fill_keys ($keys, "array value");

Exchange key names and values using the ARRAY_FILP () function

$speed = Array_flip ($speed);

Replace the 6th cell with a value of 7 using the Array_splice () function

$output = Array_splice ($input, 6,0,7);

Use the Array_splice () function to delete an array cell, preserving only the first 5 cells

$output = Array_splice ($input, 5);

$array 1 = range (10,100,10); Use the third parameter of the range () function to set the stepping value between cells

※ Sort

Shuffle ($array);//Disturb the order of the array

Use Array_multisort () to sort three of arrays

Array_multisort ($sort 1, $sort 2, $sort 3);

Sort the array and keep the index relationship

Asort ($array);

Reverse-sort the test array and keep the index relationship

Arsort ($array);

Use Ksort () to sort the key names of an array

Ksort ($array);

Reverse-order using the Krsort () function key Name

Krsort ($array);

Sort the test array using sort () [by Key name]

Sort ($array);

Use Natsort () to sort [natural sort, arrange numerically] to be sensitive to cell value case

Natsort ($array);

Sort by using the Natcasesort () function [natural sort] but ignoring numeric capitalization

Natcasesort ($array);

Use the Array_reverse () function to sort the array cells in reverse order

$newArray = Array_reverse ($array, TRUE);//true preserve the original key name when set

※ Intersection, Difference set

Using Array_diff () to calculate the difference set of three arrays [logarithmic group numerical comparison]

$result = Array_diff ($dog 1, $dog 2, $dog 3);

Use ARRAY_DIFF_ASSOC () to calculate the difference of three arrays [comparison of numeric values and key names]

$result = Array_diff_assoc ($dog 1, $dog 2, $dog 3);

Using Array_diff_key () to calculate the difference of three arrays [comparison key names]

$result = Array_diff_key ($dog 1, $dog 2, $dog 3);

Use Array_intersect () to calculate the intersection of three arrays [logarithmic group numeric comparison]

$result = Array_intersect ($dog 1, $dog 2, $dog 3);

Use ARRAY_INTERSECT_ASSOC () to calculate the intersection of three arrays [comparison of numeric and key names]

$result = Array_intersect_assoc ($dog 1, $dog 2, $dog 3);

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

$result = Array_intersect_key ($dog 1, $dog 2, $dog 3);

※ Merging arrays

Merging arrays with the Array_merge () function

$result = Array_merge ($array 1, $array 2, $array 3, $array 4, $array 5);

Array_rand ($input, 10);//random removal of 10 units

Count ($array, count_recursive);//display array unit number, 2 parameter can only be 1 or count_recursive, sometimes can traverse multidimensional array

※ Access Stack

Array out of the stack, LIFO, the last element of the array pops up

Array_pop ($array);

Array into the stack, adding 7, 82 values to the tail of the array

Array_push ($array, 7,8);

Move array start cell to group

Array_shift ($array);

Add 7, 8 to the beginning of the array

Array_unshift ($array, 7,8);

http://www.bkjia.com/PHPjc/631335.html www.bkjia.com true http://www.bkjia.com/PHPjc/631335.html techarticle what is an array of arrays 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. Two 、...

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