About Arrays:
Arrays in PHP are complex and more flexible than arrays in many other advanced languages.
Array array is a group of Ordered Variables, each of which is called an element.
The array can be numbered or associated, that is, the elements of the array can be accessed in PHP based on the numerical index or normalized String respectively. The array can contain scalar (integer, Boolean, String, floating point number) or composite values (objects or even other arrays), and can contain different types of Values
1. Create an array
PHP provides the Array () Language Structure for creating Arrays
$ Numbers = array (5, 4, 3, 2, 1 );
$ Words = array ("Web", "Database", "application ");
Echo $ numbers [2];
Echo $ words [0];
--------------------- Output result ----------------------------------------
3web
---------------------------------------------------------------------
By default, the index of the first element of the array is 0. You can use the square brackets [] syntax to retrieve and modify the values contained in the array.
$ Numbers [5] = 0;
The array of the digital index can be created from any index value.
$ Numbers = array (1 => "one", "two", "three", "four ");
You can also use loose indexes.
$ Numbers = array (1 => "one", 3 => "three", 5 => "zero ");
You can create an empty array by assigning an array () with no parameters to the variable. Then you can add the value by using the square brackets [] syntax.
$ Error = array ();
$ Error [] = "no error !!! ";
$ Error [] = "second error !!! ";
Echo $ error [0];
Echo $ error [1];
--------------------- Output result ----------------------------------------
No error !!! Second Error !!!
---------------------------------------------------------------------
2. Join an array
Associative array uses string indexes (or keys) to access the values stored in the array.
Arrays of correlated indexes are very useful for database layer interaction.
$ Newarray = array ("first" => 1, "second" => 2, "third" => 3 );
Echo $ newarray ["second"];
$ Newarray ["third"] = 5;
Echo $ newarray ["third"];
--------------------- Output result ----------------------------------------
25
---------------------------------------------------------------------
3. Heterogeneous Array
PHP Arrays can contain different types of Values
$ Mixedbag = array ("cat", 42, 8.5, false );
Var_dump (mixedbag );
4. Multi-dimensional array
Create an array containing arrays. There is no limit on the array dimension, but it is generally hard to imagine an array with more than three dimensions.
$ Planets = array (Array ("mm",), array ("Nn",), array ("BB",), array ("VV ", 7, 8 ));
Print $ planets [2] [0];
$ Planets2 = array ("mm" => array ("AA" => 1, "SS" => 2 ),
"Nn" => array ("DD" => 3, "FF" => 4 ),
"BB" => array ("GG" => 6, "hh" => 7, "PP" => array ("Haha !!!! ")),
"VV" => array ("JJ" => 6, "KK" => 7, "ll" => array ("one", "two ")));
Print $ planets2 ["VV"] ["ll"] [0];
--------------------- Output result ----------------------------------------
Bbone
---------------------------------------------------------------------
5. Use the loop to access the Array
There are many ways to traverse the Array (using the while, for loop) or foreach statement, the easiest is the foreach statement
$ Length = array (0,107,202,400,475 );
// Convert centimeter to inch
For ($ I = 0; $ I <5; $ I ++)
{
Echo ($ length [$ I]/3 );
}
$ J = 0;
While (isset ($ length [$ J])
{
Echo ($ length [$ J]/3 );
$ J ++;
}
Foreach ($ length as $ cm)
{
Echo ($ cm/3 );
}
--------------------- Output result ----------------------------------------
0
35.6666666667
67.3333333333
133.333333333
158.333333333
0
35.6666666667
67.3333333333
133.333333333
158.333333333
0
35.6666666667
67.3333333333
133.333333333
158.333333333
---------------------------------------------------------------------
Foreach can also iterate the values of associated Arrays
$ Sound = array ("cow" => "moo", "dog" => "woof ",
"Pig" => "oink", "Duck" => "quack ");
Foreach ($ sound as $ animal => $ noice)
{
Echo "$ animal sounds like this $ noice ......";
}
--------------------- Output result ----------------------------------------
The cow call is moo ......
The cry of dog is such a woof ......
The call of pig is such an oink ......
The duck sounds like quack ......
---------------------------------------------------------------------
6. Use an array pointer
Together with the keys and associated values stored in the array, PHP also has an internal index pointing to the current element of the array,
Several functions use and update the array index to provide access to array elements.
$ A = array ("A", "B", "C", "D", "E", "F ");
Echo current ($ );
Each ($ );
Key ($ A); // the pointer to the current array, returns its index
Echo current ($ A); // the value of the current element
Each ($ A); // returns the value of the current element and points the internal index to the next element.
Each ($ );
Echo current ($ );
Next ($ A); // point to the next element
Echo current ($ );
Prev ($ A); // point to the previous Element
Echo current ($ );
End ($ A); // point to the last element
Echo current ($ );
Key ($ );
Echo current ($ );
--------------------- Output result ----------------------------------------
Abdedff
---------------------------------------------------------------------
7. Basic array Functions
Integer count (mixed var) // returns the number of elements in the array, which can also be used for any variable
Number max (array numbers) // returns the maximum value in the array.
Number min (array numbers) // returns the minimum value in the array.
Boolean in_array (mixed needle, array haystack [, bollean strict]) // searches for values in the array. The third parameter is optional and the type check is enforced.
Mixed array_search (mixed needle, array haystack [, Boolean strict]) // The Return key instead of a Boolean value,
If no value is found, false is returned. If the first element is found, 0 is returned, and PHP automatically converts it to false. Therefore, you need to use ===to judge the result, as shown below:
$ A = array ("A", "B", "C", "D", "E", "F ");
$ Index = array_search ("A", $ );
If ($ Index = false)
Echo "no character 'a' is found in array '";
Else
Echo "Index = $ Index ";
--------------------- Output result ----------------------------------------
Index = 0
---------------------------------------------------------------------
Array array_reverse (array source [, bool preserve_keys]) // reverse the array to generate a new array. If the optional parameter is true, the association between indexes and elements is retained.
$ A = array ("A", "B", "C", "D", "E", "F ");
$ Newa = array_reverse ($ A); // flip directly
Echo $ newa [0];
$ Newb = array_reverse ($ A, true); // The association between indexes and elements is retained.
Echo $ newb [0];
--------------------- Output result ----------------------------------------
Fa
---------------------------------------------------------------------
Sort (array subject [, integer sort_flag]) // sort the elements in ascending order (in the original array)
Rsort (array subject [, integer sort_flag]) // sort the elements in descending order by value (in the original array)
The optional parameter can be set to sort_numeric by number, sort_string by string, or sort_regular by string.
Sort () and rsort () can be used to associate arrays, but the keys will be lost.
Asort (array subject [, integer sort_flag]) // sort the elements in ascending order of values (in the original array) to maintain key-value Association
Arsort (array subject [, integer sort_flag]) // sort the elements in descending order by value (in the original array) to maintain key-value Association
When asort () arsort () is used for a non-correlated array, the elements are re-arranged in the order of sorting, but the index of the accessed elements is not changed.
Integer ksort (ayyay subject [, integer sort_flag]) // sort by key or index in ascending order
Integer krsort (ayyay subject [, integer sort_flag]) // sort by key or index in descending order
Usort (array subject, string compare_function) // you can define your own sorting rule function based on user-defined sorting, but the function must comply
Uasort (array subject, string compare_function) // integer my_compare_function (mixed a, mixed B), A> B returns 1, A uksort (array subject, string compare_function) // if A is equal to B, 0 is returned.
$ Numbers = array (2,171 );
Sort ($ numbers );
Foreach ($ numbers as $ N)
Echo $ n ."";
$ Numbers = array (2,171 );
Rsort ($ numbers );
Foreach ($ numbers as $ N)
Echo $ n ."";
$ Numbers = array (2,171 );
Sort ($ numbers, sort_string );
Foreach ($ numbers as $ N)
Echo $ n ."";
$ A = array ("O" => "KK", "E" => "ZZ", "Z" => "hh ", "A" => "RR ");
Asort ($ );
Foreach ($ A as $ keyname => $ keyValue)
Echo $ keyValue;
$ A = array ("O" => "KK", "E" => "ZZ", "Z" => "hh ", "A" => "RR ");
Ksort ($ );
Foreach ($ A as $ keyname => $ keyValue)
Echo $ keyValue;
// Compare two strings Based on the length
Function cmp_length ($ A, $ B)
{
If (strlen ($ A) <strlen ($ B ))
Return-1;
If (strlen ($ A)> strlen ($ B ))
Return 1;
Return 0;
}
$ Animals = array ("cow", "Ox", "monkey", "Mimi ");
Usort ($ animals, "cmp_length ");
Foreach ($ animals as $)
Echo $;
--------------------- Output result ----------------------------------------
2 3 5 6 16 19 24 171 171 24 19 16 6 5 3 2 16 171 19 2 24 3 5 6 hhkkrrzzrrzzkkhhoxcowmimonkey
---------------------------------------------------------------------
Array_merge ($ A, $ B) // composite array. One of the interesting functions is to generate an array from two arrays, and the values with the same key will be overwritten.
/// // Code ////////// //////////////////////////////
$ A = array ("name" => "zhangsan", 10,100 );
$ B = array ("name" => "Lisi", 4,6, 8 );
$ C = array_merge ($ A, $ B );
Var_dump ($ C );
//////////////////////////////////////// //////////////////////////////////
--------------------- Output result ----------------------------------------
Array (6) {["name"] => string (4) "Lisi" [0] => int (10) [1] => int (100) [2] => int (4) [3] => int (6) [4] => int (8 )}
---------------------------------------------------------------------
Array array_combine (array $ A, array $ B) // The value of array a is the key of the new array, and the value of array B is the value of the new array. If the array length is different, false is returned.
/// // Code ////////// //////////////////////////////
$ A = array ("name", "math", "China ");
$ B = array ("zhangsan", 4,6 );
$ C = array_combine ($ A, $ B );
Var_dump ($ C );
//////////////////////////////////////// //////////////////////////////////
--------------------- Output result ----------------------------------------
Array (3) {["name"] => string (8) "zhangsan" ["math"] => int (4) ["China"] => int (6 )}
---------------------------------------------------------------------