The example in this article summarizes the usage of PHP arrays. Share to everyone for your reference, specific as follows:
demo1.php
<?php
//Create an array variable
$userNames = Array (' John ', ' Dick ', ' Harry ');
Print out the array
//echo $userNames;//array
//$userName = ' John ';
echo $userName//John
///If you want to print out an element of this array//
then you must find the subscript for this element, key (key)
//0,1,2
//echo $userNames [2];/ /Harry
//print_r--Print easy to understand information about variables.
//print_r ($userNames);//array ([0] => John [1] => dick [2] => Harry)
//$userNames is an array variable, and $userNames [x] Can To understand the small variables under the multivariate
$userNames [4] = ' Zhao Qi ';
Print_r ($userNames);//array ([0] => John [1] => dick [2] => Harry [4] => Zhao VII)
?>
demo2.php
<?php
//range--Creates an array containing the specified range of cells
//range contains the specified array
//contains two things, one called a key, and a value
//key is automatically generated, Default starting from 0, each time +1
//value is your own assigned value
//$numbers = range (1,4);
Print_r ($numbers); Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)
//$letters = range (' A ', ' e ');
Print_r ($letters);//array ([0] => a [1] => b [2] => C [3] => D [4] => E)
//Echo $letters [3];
?>
demo3.php
<?php
$userNames = Array (' John ', ' Dick ', ' Harry ');
Loop to display all values in the array
//for ($i =0 $i <3; $i + +) {
// Echo ($i + 1). ' --> '. $userNames [$i]. ' <br/> ';
}
* * 1--> John
* 2--> Dick *
3--> Harry
*/
/Echo count ($userNames);//3
//For ($i =0; $i <count ($userNames); $i + +) {
// Echo ($i + 1). ' --> '. $userNames [$i]. ' <br/> ';
//If the key does not start at 0, or is not a number at all, then it is not possible to use a listless loop to display the list of data or to
traverse the array through a Foreach loop, which is beneficial without the need to consider key
//foreach ($userNames as $value) {
// echo $value. ' <br/> ';
//foreach traversal $key => $value
//foreach ($userNames as $keyaaa => $value) {
// echo $keyaaa. ' --> '. $value. ' <br/> ';
/** * 0--> John * 1--> Dick * 2--> Harry * * * * * * * * * */
/So, you have to make a judgment first
if (Is_array ($ UserNames)) {
foreach ($userNames as $key => $value) {
echo $key. '--> '. $value. ' <br/> ';
}
} else{
echo $userNames;
}
? >
demo4.php
<?php
$userNames = Array (' John ', ' Dick ', ' Harry ');
Print_r ($userNames);
echo $userNames [0];
? >
demo5.php
<?php//
Create a custom key (key) array
//If you do not declare the element key, then compute
//$userNames = Array (' Baidu ' => ' John ', ' Dick ', ' Harry ') from start 0;
//Print_r ($userNames);//array ([Baidu] => John [0] => dick [1] => Harry)
$userNames = Array (' Baidu ' => ') John ', ' Taobao ' => ' Dick ', ' 360 ' => ' Harry ');
Print_r ($userNames); Array ([Baidu] => John [Taobao] => Dick [360] => Harry)
Echo $userNames [' Baidu '];
? >
demo6.php
<?php
//First create an array of only one element
$userAge = Array (' CHAOYV ' =>25);
Print out the Chaoyv age
//echo $userAge [' chaoyv '];//25
//Add the previous array, append two, here is the subscript, key, key is a thing
$userAge [' yike '] = ;
$userAge [' wife '] =;
Print_r ($userAge);
? >
demo7.php
<?php
//array keyword can not be, you can create an array
$userAges [' chaoyv '] =;
Print_r ($userAges); Array ([chaoyv] =>)
$userAges [' yike '] =;
$userAges [' wife '] =;
Print_r ($userAges); Array ([chaoyv] => [yike] => [wife] =>)
//You cannot use a for loop to display all of the data, and you can only traverse it through a foreach
( $userAges as $value) {
echo $value. ' <br/> ';
}
? >
demo8.php
<?php
//$userAges [' chaoyv '] =;
$userAges [' yike '] =;
$userAges [' wife '] =;
$username = Array (' => '), ' blood ' => ' Zhao Blood son ', ' learn ' => ' Mo Study ');
Print_r ($username);
each use//each-returns the current key/value pair in the array and moves the array pointer forward//
There is a pointer, by default, the pointer is the first key value pair
//The first key value right here is ' the world ' => ' how to open '
//If each ($username), then gets the first key value to the ' world ' => ' How to open '
//each This function returns an array,
//each gets the first key value pair, and then wraps it into a new array.
//print_r (each ($username));
Equivalent to $a = Array ([1] => how to open [value] => where [0] => World [key] =>)
//$a = each ($username);
echo $a [value];
Print_r (each ($username));
Echo ' <br/> ';
Print_r (each ($username));
? >
demo9.php
<?php
$username = Array (' => '), ' blood ' => ' Zhao Blood son ', ' learn ' => ' Mo Study ');
Here, how do we use each to loop through all the data?
//equivalent to $a = Array ([1] => where [value] => open [0] => [key] =>)
//Two exclamation point, representing real data converted to Boolean
//Echo !! each ($username)//indicates that there is data, there is data, the idea of Boolean values is True (TRUE)
//echo!! each ($username);
Echo!! each ($username);
Echo!! each ($username);//The fourth is a false while
(!! $a = each ($username)) {
echo $a [' key ']. ' --> '. $a [' value ']. ' <br/> ';
}
$a = each ($username);
echo $a [0]. ' ---'. $a [1]. ' <br/> ';
$a = each ($username);
echo $a [0]. ' ---'. $a [1]. ' <br/> ';
$a = each ($username);
echo $a [0]. ' ---'. $a [1]. ' <br/> ';
/**
//* World---How to open
//* blood---zhao blood son//* Learn---MO study/
/* * * *
?>
demo10.php
<?php
//$usernames = Array (' World ' => ' How to open ', ' blood ' => ' Zhao Blood son ', ' learn ' => ' Mo Study ');
$a = each ($usernames);
World--> Open
//echo $a [' key '];
Echo '--> ';
echo $a [' value '];
$a = each ($usernames);
Blood--> Zhao blood son
//echo $a [' key '];
Echo '--> ';
echo $a [' value '];
List--assigns the values in the array to some variables
//$a = array (' AAA ', ' BBB ', ' CCC ', ' ddd '); Print_r ($a);//array ([0] => AAA [1] => BBB [2] => CCC [3] => DDD)
//List ($var 1, $var 2, $var 3,$ VAR4) = $a;
echo $var 4;
$usernames = Array (0=> ' How to open ', ' blood ' => ' Zhao Blood son ', ' learn ' => ' Mo Study ');
List can only recognize that the key is numeric////////The
custom string key is not able to use list to identify
//list ($a, $b, $c) = $usernames;
echo $a//why open
$usernames = Array (' => ', ' => ', ' blood ', ' Zhao Blood son ', ' learn ' => ' Mo Study ');
Equivalent to $a = Array ([1] => where to open [value] => [0] => [key] =>)
list ($name, $username) = each ($usernames) ;
echo $username;
? >
demo11.php
<?php
$usernames = Array (' => '), ' blood ' => ' Zhao Blood son ', ' learn ' => ' Mo Study ');
$a =each ($usernames);
echo $a [key];
$a =each ($usernames);
echo $a [key];
For the third time, I want to take the first array of the array
//As long as the pointer of the array is adjusted to the first position
//reset--point the inner pointer of the array to the first unit
reset ($usernames);
$a = each ($usernames);
echo $a [key];
World Blood
?>
demo12.php
<?php
//$usernames = Array (' World ' => ' How to open ', ' boundary ' => ' How to open ', ' blood ' => ' Zhao Blood son ', ' learn ' => ' mo xue ');
Print_r ($usernames);
Echo ' <br/> ';
Array_unique--The duplicate value in the group is removed////
A new array is created, and the new array has been removed, and the old array is now
//$a = Array_unique ($usernames);
Print_r ($a);
$numbers = Array (1,24,2,1,3,4,2,6,4,2,4,56,2,4,5);
Print_r ($numbers);
$newNumbers = Array_unique ($numbers);
Print_r ($newNumbers);
? >
demo13.php
<?php
$usernames = Array (' => ', ' How to open ', ' boundary ' => ' where ', ' blood ' => ' Zhao Blood son ', ' learn ' => ' mo xue ');
Print_r ($usernames);
Echo ' <br/> ';
Array_flip--the key and value
$newUsernames = Array_flip ($usernames) in the commutative array;
Print_r ($newUsernames);
/**
* Array ([Shi] => how to open [the] => where [blood] => zhao Xue son [xue] => mo xue)
* Array ([where to open] => [Zhao blood son] => blood [mo xue] = >)
* */
?>
demo14.php
<?php
//Create a single array
$products = Arrays (
' Apple ', ' 6 ', ' 28.8 '),
array (' Pork ', ' 2 ', ' 18.8 '),
Array (' Cookies ', ' 4 ', ' 48.8 ')
);
Print_r ($products);
Take $products first element out of the
//print_r ($products [2]);
Array of Arrays
echo "|". $products [0][0]. "|". $products [0][1]. "|". $products [0][2]. "| <br/> ";
echo "|". $products [1][0]. "|". $products [1][1]. "|". $products [1][2]. "| <br/> ";
echo "|". $products [2][0]. "|". $products [2][1]. "|". $products [2][2]. "| <br/> ";
? >
demo15.php
<?php
//Create a single array
$products = Arrays (
' Apple ', ' 6 ', ' 28.8 '),
array (' Pork ', ' 2 ', ' 18.8 '),
Array (' Cookies ', ' 4 ', ' 48.8 ')
);
The length of the outer array is first required
//echo count ($products);
For ($i =0 $i <count ($products); $i + +) {for
($j =0; $j <count ($products [$i]); $j + +) {
echo ' | '. $products [$i] [$j];
}
echo "|<br/>";
}
* * * Apple |6|28.8|
* | pork |2|18.8|
* | Biscuit |4|48.8|
* **/
?>
demo16.php
<?php
//Create a single array
$products = arrays (
' product ' => ' apple ', ' Quantity ' => ' 6 ', ' Price ' => ' 28.8 '),
array (' Product ' => ' pork ', ' Quantity ' => ' 3 ', ' Price ' => ' 25.8 '),
Array (' Product ' => ' cookies ', ' Number ' => ' 2 ', ' Price ' => ' 26.8 '))
;
Print_r ($products) ;
For ($i =0 $i <count ($products); $i + +) {
// foreach ($products [$i] as $key => $value) {
// echo $key. '-$value. ' | }
// echo ' <br/> ';
For
($i =0 $i <count ($products); $i + +) {while
!! List ($key, $value) =each ($products [$i])) {
echo $key. '--'. $value. ' | ';
}
Echo ' <br/> ';
}
/**
* Products--Apple | quantity--6| price--28.8|
* Products--pork | Quantity--3| price--25.8|
* Products-Biscuit | Quantity--2| price--26.8|
* * * */
?>
demo17.php
<?php
////sort--array sorting
//$fruit = array (' banner ', ' Orange ', ' apple ');
Before ordering, it is usually shown
////print_r ($fruit) in the order of key;
Sort after
//Sort ($fruit);
Print_r ($fruit);
$numbers = Array (45,44,27,574,241,7,45,1,5,2,4,5);
Print_r ($numbers);
Rsort ($numbers);
Echo ' <br/> ';
Print_r ($numbers);
According to the number, to see the size of the overall number, according to the string, only the first bit size
//$numbers = Array (2,12,3);
Sort ($numbers, sort_numeric);
Print_r ($numbers); Array ([0] => 2 [1] => 3 [2] =>)
//$numbers = Array (2,12,3);
Sort ($numbers, sort_string);
Print_r ($numbers);//array ([0] => [1] => 2 [2] => 3)
?>
demo18.php
<?php
//asort--Sort the array and keep the index relationship
$fruit = array (' banner ', ' Orange ', ' apple ');
Sort ($fruit);
Print_r ($fruit);
Array ([0] => Apple [1] => banner [2] => Orange)
asort ($fruit);
Print_r ($fruit);
Array ([2] => Apple [0] => banner [1] => orange)
?>
demo19.php
<?php
$fruit = Array (' B ' => ' banner ', ' O ' => ' orange ', ' A ' => ' apple ');
Ksort--the array is sorted according to the key name
Ksort ($fruit) ;
Print_r ($fruit);
Array ([a] => Apple [b] => banner [o] => orange)
?>
demo20.php
<?php
//echo ' ';
Create an array
$pic = Array (' mm1.jpg ', ' mm2.jpg ', ' mm3.jpg ', ' mm4.jpg ', ' mm5.jpg ');
The array is randomly disturbed
//shuffle ($pic);
Most groups reverse-sort, and the array-preceded function typically creates a new array
$a = Array_reverse ($pic);
For ($i =0 $i <3; $i + +) {
echo '
demo21.php
<?php
$userName = Array (' john ');
Print_r ($userName);
The return value of this function will get the number of elements in the current array
//array_unshift-inserting one or more cells at the beginning of the array
//inserting data at the beginning
array_unshift ($userName, ' Dick ') ;
Inserts data at the end
of the Array_push ($userName, ' Wu-ran ');
Deletes the beginning element
Array_shift ($userName);
Deletes the ending element
Array_pop ($userName);
Print_r ($userName);
? >
demo22.php
<?php
$fruit = array (' banner ', ' Orange ', ' apple ');
This function is used to get the key in an array (key)
//The second parameter indicates that the fetch of several
//$a = Array_rand ($fruit, 1);
echo $fruit [$a];
$a = Array_rand ($fruit, 2);
echo $fruit [$a [0]]. ' '. $fruit [$a [1]];
? >
demo23.php
<?php
$username = Array (' => '), ' blood ' => ' Zhao Blood son ', ' learn ' => ' Mo Study ');
By default, the pointer is in the first Data
//Gets the current element of the pointer, and not moves the pointer to the next
//Echo present ($username);
Echo current ($username);
Echo current ($username);
Echo Next ($username);
Echo current ($username);
Echo Next ($username);
Echo current ($username);
Reset--Points the array's internal pointer to the first unit
//Echo Reset ($username);
echo sizeof ($username);//count
$numbers = Array (1,4,5,656,7,5,7,4,7,4,5,7);
Array_count_values--The number of occurrences of the values in the statistical array
Print_r (Array_count_values ($numbers));
? >
demo24.php
<?php
$a = $b = $c = ';
$fruits = Array (' A ' => ' Apple ', ' B ' => ' banner ', ' C ' => ' orange ');
The string key (key) is set to a variable by a scalar function, and the value is assigned to the variable
extract ($fruits);
echo $a;
echo $c;
echo $b;
Appleorangebanner
?>
More interested in PHP related content readers can view the site topics: "PHP array" Operation Tips Encyclopedia, "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.