This example summarizes the usage of PHP arrays. Share to everyone for your reference, as follows:
demo1.php
<?php //Create an array variable $userNames = Array (' Zhang San ', ' John Doe ', ' Harry '); Print this array out//echo $userNames;//array//$userName = ' Zhang San ';//echo $userName;//Zhang San //If you want to print out an element of this array // Then you have to find the subscript for this element, key (key) //0,1,2 //echo $userNames [2];//Harry //print_r--print easy-to-understand information about the variable. //print_r ($userNames);//array ([0] = Zhang San [1] = John Doe [2] = = Harry) //$userNames is an array variable, and $userNames [x] Can The small variable under the array variable $userNames [4] = ' Zhao Qi '; Print_r ($userNames);//array ([0] = Zhang San [1] = John Doe [2] = = Harry [4] + Zhao VII)?>
demo2.php
<?php //range--Create an array containing the specified range of cells //range contains the specified array //contains two things, one is called the key (key), a value //key is automatically generated, By default starting from 0, each time +1 //value is your own assignment //$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 (' Zhang San ', ' John Doe ', ' Harry '); Loop to display all values in the array //from 0-5 cycles//for ($i =0; $i <3; $i + +) {// echo ($i + 1). ' --$userNames [$i]. ' <br/> '//} / * 1--> Zhang San * 2--> John Doe * 3--> Harry * *///Echo count ($userNames);//3//for ($ I=0, $i <count ($userNames), $i + +) {// echo ($i + 1). ' --$userNames [$i]. ' <br/> ';/ /}//If key is not starting from 0, or if it is not a number at all, then it is not possible to use the listless loop to implement the display data list //iterate through the array through a Foreach loop, which does not need to be considered key// foreach ($userNames as $value) {// echo $value. ' <br/> ';//} //foreach traverse $key + $value//foreach ($userNames as $KEYAAA + $value) {// echo $keyaaa. '--$value. ' <br/> '//} /** * 0--> Zhang San * 1--> John Doe * 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 (' Zhang San ', ' John Doe ', ' Harry '); Print_r ($userNames); echo $userNames [0];? >
demo5.php
<?php //Create an array of custom keys (key) //If you do not go to declare element key, then calculate from start 0//$userNames = Array (' Baidu ' = ' Zhang San ', ' John Doe ', ' Harry ');// Print_r ($userNames); Array ([Baidu] = Zhang San [0] = John Doe [1] = Harry) $userNames = Array (' Baidu ' = ' Zhang San ', ' taobao ' = ' John Doe ', ' the ' => ' ;' Harry '); Print_r ($userNames); Array ([Baidu] = Zhang San [taobao] [John Doe] [] = Harry) echo $userNames [' Baidu ']; >
demo6.php
<?php //First create an array of only one element $userAge = Array (' CHAOYV ' =>25); Print out the age of Chaoyv //echo $userAge [' chaoyv '];//25 //Add the previous array, append two, and here say subscript, key, key is a thing $userAge [' yike '] = ; $userAge [' wife '] =; Print_r ($userAge);? >
demo7.php
<?php //array keyword can not, you can create an array $userAges [' chaoyv '] =; Print_r ($userAges); Array ([chaoyv] [=] +) $userAges [' yike '] =; $userAges [' wife '] =; Print_r ($userAges); Array ([CHAOYV] [yike] = [wife] (=) ///There is no way to use the For loop to display all the data, but only through a foreach traversal ( $userAges as $value) { echo $value. ' <br/> '; }? >
demo8.php
<?php//$userAges [' chaoyv '] = 25;//$userAges [' yike '] = 30;//$userAges [' wife '] =; $username = Array (' world ' = ' and ' open ', ' blood ' = ' zhao Xue ', ' learn ' and ' Mo Xue '); Print_r ($username); each uses//each--Returns the current key/value pair in the array and moves the array pointer forward one step //There is a pointer, by default, the pointer is the first key value pair //Here the first key value pair is ' world ' + ' how open ' //If each ($username), then get the first key value to '//each ' and ' open ' the function returns an array, //each the first key-value pair, and wraps it into a new array. //print_r (each ($username)); Equivalent to $a = Array ([1] = where open [value] and where [0] = [key] = world) //$a = each ($username); echo $a [value]; Print_r (each ($username)); Echo ' <br/> '; Print_r (each ($username));? >
demo9.php
<?php $username = Array (' world ' = ' and ' he ', ' blood ' = ' Zhao blood ', ' learn ' and ' Mo Xue '); Here, how do we use each to loop all the data? //equal to $a = Array ([1] = where [value] = [0] = [key] = world) //Two exclamation point, which indicates that the data that really exists is converted to Boolean//echo!! each ($username);//Description There is data, there is data, with the idea of Boolean value is True (TRUE)//echo!! each ($username);//echo!! each ($username);//echo!! each ($username);//fourth is 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// * Learn---mo xue// * */?>
demo10.php
<?php//$usernames = Array (' World ' = ' ", '" ", ' blood ' = ' zhao Xue '), ' learn ' and ' Mo Xue ');//$a = each ($usernames);////----/Echo $a [' key '];//echo '--',//echo $a [' value '];//$a = each ($usernames);///Blood-to-Zhao blood//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] =& Gt AAA [1] = BBB [2] = CCC [3] = = DDD)//list ($var 1, $var 2, $var 3, $var 4) = $a;//echo $var 4;//$usernames = Array (0 = ' How to open ', ' blood ' and ' Zhao Blood ', ' learning ' and ' xue ', '////list can only know the key for the number of////////The custom string key is unable to use list to identify the//list ($a, $b, $c) = $usernam es;//echo $a;//where open $usernames = array (' world ' = ' and ' he ', ' blood ' = ' Zhao blood ', ' learning ' and ' ink Learning '); Equivalent to $a = Array ([1] = where [value] and where open [0] = [key] (the world) list ($name, $username) = each ($usernames); echo $username;? >
demo11.php
<?php $usernames = Array (' world ' = ' and ' he ', ' blood ' = ' Zhao blood ', ' learn ' and ' Mo Xue '); $a =each ($usernames); echo $a [key]; $a =each ($usernames); echo $a [key]; The third time, I want to take the first array of arrays //As long as the array pointer is adjusted to the first position can be //reset--the array's internal pointer to the first cell reset ($usernames); $a = each ($usernames); echo $a [key]; The World Blood World?>
demo12.php
<?php//$usernames = Array (' world ' = ' = ' and ' where ', ' environment ' = ' ' where ', ' blood ' = ' Zhao blood ', ' learning ' and ' ink Learning ');//Print_r ($usernames);//Echo ' <br/> ';////array_unique--Removes duplicate values from the array///Creates a new one, and the new array has been removed, the old array intact//$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 (' the world ' = ' and ' where ', ' the ' ' and ', ') ', ' blood ' = ' Zhao blood ', ' learning ' and ' ink Learning '); Print_r ($usernames); Echo ' <br/> '; Array_flip--Switches the keys and values in the array $newUsernames = Array_flip ($usernames); Print_r ($newUsernames); /** * Array ([world] = why open [border] and how open [blood] = Zhao blood son [study] = Ink learning) * Array ([where open] = [Zhao blood son] = blood [mo xue] = > Study) * */?>
demo14.php
<?php //Create a single array $products = Array ( ' Apple ', ' 6 ', ' 28.8 '), array (' Pork ', ' 2 ', ' 18.8 '), Array (' Biscuit ', ' 4 ', ' 48.8 ') ); Print_r ($products); Take $products first element out of the //print_r ($products [2]); Array in array echo "|". $products [0][0]. "|". $products [0][1]. "|". $products [0][2]. "| <br/> "; echo "|". $products [1][0]. "|". $products [1][1]. "|". $products [All in all]. "| <br/> "; echo "|". $products [2][0]. "|". $products [2][1]. "|". $products [2][2]. "| <br/> ";? >
demo15.php
<?php //Create a single array $products = Array ( ' Apple ', ' 6 ', ' 28.8 '), array (' Pork ', ' 2 ', ' 18.8 '), Array (' Biscuit ', ' 4 ', ' 48.8 ') ); First, the length of the outer array is requested //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 = Array ( ' product ' = ' apple ', ' quantity ' = ' 6 ', ' price ' = ' 28.8 '), array (' product ' = ' pork ', ' quantity ' = ' 3 ', ' price ' = ' ' 25.8 '), Array (' product ' = ' cookie ', ' quantity ' = ' 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-Biscuits | Quantity--2| price--26.8| * */?>
demo17.php
<?php////sort--array sort//$fruit = array (' banner ', ' Orange ', ' apple '),///////////print_r ($fruit) in order of the key before sorting ;////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 overall number size, 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 ($number s);//array ([0] = [1] = 2 [2] = 3)?>
demo18.php
<?php//asort-Sorts the array and keeps 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 by 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 are reversed, and a function preceded by an array typically creates a new array $a = Array_reverse ($pic); for ($i =0; $i <3; $i + +) { echo ' '; echo "\ n"; }? >
demo21.php
<?php $userName = Array (' Zhang San '); Print_r ($userName); The return value of this function will be given, the number of elements of the current array //array_unshift--insert one or more cells at the beginning of the array //Insert data at the beginning array_unshift ($userName, ' John Doe ') ; Insert data at the end Array_push ($userName, ' Wu '); Delete the beginning element Array_shift ($userName); Delete the end of the 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 a few//$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 (' world ' = ' and ' he ', ' blood ' = ' Zhao blood ', ' learn ' and ' Mo Xue '); By default, the pointer is in the first Data //Gets the current element of the pointer, and currently does not move the pointer to the next//Echo present ($username);//echo $username;//Echo Current ($username);//Echo Next ($username);//Echo current ($username)//Echo Next ($username);//Echo Current ($ username);////reset--Points the internal pointer of the array 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 all values in the statistic 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?>
I hope this article is helpful to you in PHP programming.