This article mainly introduces the array usage in the PHP getting started tutorial, and summarizes and analyzes common php operation skills on array creation, printing, traversing, acquisition, sorting, insertion, and deletion in combination with a large number of instances, for more information about PHP array usage, see the examples in this article. We will share this with you for your reference. The details are as follows:
Demo1.php
Zhang San [1] => Li Si [2] => Wang Wu) // $ userNames is an array variable, $ userNames [x] can be considered as a small variable under the array variable $ userNames [4] = 'Zhao 7'; print_r ($ userNames ); // Array ([0] => Zhang San [1] => Li Si [2] => Wang Wu [4] => Zhao Qi)?>
Demo2.php
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
'. $ UserNames [$ I].'
'; //}/** 1 --> Zhang San * 2 --> Li Si * 3 --> Wang Wu ** // echo count ($ userNames ); // 3 // for ($ I = 0; $ I
'. $ UserNames [$ I].'
'; //} // If the key does not start from 0, or is not a number at all, you cannot use a listless loop to display the data list. // you can use a foreach loop to traverse the array, this method is advantageous because you do not need to consider key // foreach ($ userNames as $ value) {// echo $ value.'
'; //} // Foreach traversal $ key =>$ value // foreach ($ userNames as $ keyaaa =>$ value) {// echo $ keyaaa. '--> '. $ value.'
'; //}/*** 0 --> Zhang San * 1 --> Li Si * 2 --> Wang Wu ** // therefore, make a judgment first if (is_array ($ userNames) {foreach ($ userNames as $ key => $ value) {echo $ key. '--> '. $ value.'
';}} Else {echo $ userNames ;}?>
Demo4.php
Demo5.php
'Zhang San', 'Li Si', 'Wang Wu'); // print_r ($ userNames ); // Array ([baidu] => Zhang San [0] => Li Si [1] => Wang Wu) $ userNames = array ('baidu' => 'Zhang San ', 'Taobao' => 'Li Si', '000000' => 'Wang Wu'); print_r ($ userNames ); // Array ([baidu] => Zhang San [taobao] => Li Si [360] => Wang Wu) echo $ userNames ['baidu'];?>
Demo6.php
25); // Print the chaoyv's age // echo $ userAge ['chaoyv']; // 25 // Append the previous array with two subscripts, key, key is a Things $ userAge ['yike '] = 30; $ userAge ['wife'] = 24; print_r ($ userAge);?>
Demo7.php
25) $ userAges ['yike '] = 30; $ userAges ['wife'] = 24; // print_r ($ userAges ); // Array ([chaoyv] => 25 [yike] => 30 [wife] => 24) // the for loop cannot be used to display all the data, only foreach ($ userAges as $ value) {echo $ value can be traversed through foreach.'
';}?>
Demo8.php
'Ho Kai', 'blood' => 'Zhao Xue'er ', 'learn' => 'Mo xuezhi'); // print_r ($ username ); // use each // each -- return the current key/value pair in the array and move the array pointer one step forward. // by default, pointer is to specify the first key-value pair // here the first key-value pair is 'shi' => ''// if each ($ username ), the first key-value pair 'shi' => 'He Kai' // The each function returns an array. // each obtains the first key-value pair, then it is packaged into a new array. // Print_r (each ($ username )); // equivalent to $ a = Array ([1] => He Kai [value] => He Kai [0] => World [key] => World) // $ a = each ($ username); // echo $ a [value]; print_r (each ($ username); echo'
'; Print_r (each ($ username) ;?>
Demo9.php
'Ho Kai', 'blood' => 'Zhao Xue'er ', 'learn' => 'Mo xuezhi'); // here, how can we use each to loop all data? // Equivalent to $ a = Array ([1] => [value] => [0] => [key] =>) // two exclamation points, convert existing data to a Boolean value // echo !! Each ($ username); // indicates that there is data and data. The concept of using a Boolean value is true. // echo !! Each ($ username); // echo !! Each ($ username); // echo !! Each ($ username); // The fourth is a false while (!! $ A = each ($ username) {echo $ a ['key']. '-->'. $ a ['value'].'
';} // $ A = each ($ username); // echo $ a [0].' --- '. $ a [1].'
'; // $ A = each ($ username); // echo $ a [0].' --- '. $ a [1].'
'; // $ A = each ($ username); // echo $ a [0].' --- '. $ a [1].'
'; // ** // * World --- he Kai/* blood --- Zhao Xueer/* learn --- mo Xuezhi/**/?>
Demo10.php
'Ho Kai', 'blood' => 'Zhao Xue'er ', 'learn' => 'Mo xuezhi'); // $ a = each ($ usernames ); /// world --> He Kai // echo $ a ['key']; // echo '-->'; // echo $ a ['value']; // $ a = each ($ usernames); // blood --> Zhao Xueer // echo $ a ['key']; // echo '--> '; // echo $ a ['value']; // list -- assign the values in the array to some variables // $ a = array ('AAA', 'BBB ', 'CCC ', 'ddd'); // print_r ($ ); // Array ([0] => aaa [1] => bbb [2] => ccc [3] => ddd) // list ($ var1, $ var2, $ var3, $ var4) = $ a; // echo $ var4; // $ user Names = array (0 => 'ho Kai', 'blood' => 'Zhao Xueer ', 'learn' => 'Mo Xuezhi '); //// list only recognize the string key whose key is a number. // The custom string key cannot be identified by list. // list ($ a, $ B, $ c) = $ usernames; // echo $ a; // $ usernames = array ('shi' => 'ho Kai ', 'blood' => 'Zhao Xue'er ', 'learn' => 'Ink learn '); // equivalent to $ a = Array ([1] => [value] => [0] => World [key] => world) list ($ name, $ username) = each ($ usernames); echo $ username;?>
Demo11.php
'Ho Kai', 'blood' => 'Zhao Xue'er ', 'learn' => 'Mo xuezhi'); $ a = each ($ usernames ); echo $ a [key]; $ a = each ($ usernames); echo $ a [key]; // The third time, I want to take the first array of the array. // you only need to adjust the pointer of the array to the first position. // reset -- point the internal pointer of the array to the first reset ($ usernames ); $ a = each ($ usernames); echo $ a [key]; // What is the world's blood?>
Demo12.php
'Ho Kai', 'bound' => 'ho Kai', 'blood' => 'Zhao Xueer ', 'learn' => 'Mo Xuezhi '); // print_r ($ usernames); // echo'
'; // Array_unique -- removes the repeated values in the array. // creates a new array, and the new array has been removed, the old array is intact // $ a = array_unique ($ usernames); // print_r ($ a); $ numbers = array, 2, 4, 5); print_r ($ numbers); $ newNumbers = array_unique ($ numbers); print_r ($ newNumbers);?>
Demo13.php
'Ho Kai', 'bound' => 'ho Kai', 'blood' => 'Zhao Xueer ', 'learn' => 'Mo Xuezhi '); print_r ($ usernames); echo'
'; // Array_flip -- exchange keys and values in the array $ newUsernames = array_flip ($ usernames); print_r ($ newUsernames ); /*** Array ([World] => He Kai [environment] => He Kai [blood] => Zhao Xueer [learning] => Mo Xuezhi) * Array ([he Kai] => Jing [Zhao Xueer] => Xue [moxue] => Xue) **/?>
Demo14.php
"; Echo" | ". $ products [1] [0]. "| ". $ products [1] [1]. "| ". $ products [1] [2]. "|
"; Echo" | ". $ products [2] [0]. "| ". $ products [2] [1]. "| ". $ products [2] [2]. "|
";?>
Demo15.php
";}/ ** | Apple | 6 | 28.8 | * | pork | 2 | 18.8 | * | biscuit | 4 | 48.8 | ***/?>
Demo16.php
'Apple', 'qty '=> '6', 'price' => '28. 8 '), array ('product' => 'pork', 'quantity '=> '3', 'price' => '25. 8 '), array ('product' => 'cooker', 'quantity' => '2', 'price' => '26. 8 '); // print_r ($ products); // for ($ I = 0; $ I
$ Value) {// echo $ key. '--'. $ value. '|'; //} // echo'
'; //} For ($ I = 0; $ I
';} /*** Product -- Apple | quantity -- 6 | price -- 28.8 | * product -- pork | quantity -- 3 | price -- 25.8 | * product -- biscuit | quantity -- 2 | price -- 26.8 | **/?>
Demo17.php
'; Print_r ($ numbers); // The size of the overall number depends on the number, and the size of the first digit depends on the string. // $ numbers = array (2, 12, 3); // sort ($ numbers, SORT_NUMERIC); // print_r ($ numbers ); array ([0] => 2 [1] => 3 [2] => 12) // $ numbers = array (2, 12, 3); // sort ($ numbers, SORT_STRING); // print_r ($ numbers); // Array ([0] => 12 [1] => 2 [2] => 3)?>
Demo18.php
Apple [1] => banner [2] => orange) asort ($ fruit); print_r ($ fruit ); // Array ([2] => apple [0] => banner [1] => orange)?>
Demo19.php
'Banner ', 'O' => 'Orange', 'a' => 'apple'); // ksort -- sort the array by key name ($ fruit ); print_r ($ fruit); // Array ([a] => apple [B] => banner [o] => orange)?>
Demow.php
Demo21.php
Demo22.php
Demo23.php
'Ho Kai', 'blood' => 'Zhao Xue'er ', 'learn' => 'Mo xuezhi'); // by default, pointer in the first data // get the current element of the pointer, current does not move the pointer to the next step // echo current ($ username ); // echo current ($ username); // echo next ($ username ); // echo current ($ username); // reset -- point the internal pointer of the array to the first unit // echo reset ($ username ); // echo sizeof ($ username); // count $ numbers = array (5,656,); // array _ Count_values -- count the number of occurrences of all values in the array print_r (array_count_values ($ numbers);?>
Demo24.php
'Apple', 'B' => 'banner ', 'C' => 'Orange'); // you can use a scalar function to set the string key (key) to a variable, then the value is assigned to this variable extract ($ fruits); echo $ a; echo $ c; echo $ B; // appleorangebanner?>
I hope this article will help you with PHP programming.
For more articles on array usage summary (create, delete, Traverse, and sort) in PHP Getting Started Tutorial, please follow the PHP Chinese website!