About arrays:
Arrays in PHP are complex and are more flexible than arrays in many other high-level languages.
An array of arrays is an ordered set of variables in which each variable is called an element.
Arrays can be numbered or associated, that is, the elements of an array can be accessed individually based on numeric indices or text strings.
In PHP, an array can contain scalars (integers, Boolean, string, floating-point numbers) or compound values (objects and even other arrays) and can contain different types of values
1. Create an array
PHP provides an 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----------------------------------------
3web
---------------------------------------------------------------------
By default, the index of the first element of an array is 0. The values contained in the array can be retrieved and modified by using the square brackets [] syntax
$numbers [5] = 0;
An array of digitized indexes can create bits starting from any index value
$numbers = Array (1=> "one", "two", "three", "four");
You can also loose the index
$numbers = Array (1=> "one",3=> "three",5=> "Zero");
You can create an empty array by giving the variable an array () with no parameters. You can then add values by using the brackets [] syntax
$error = Array ();
$error [] = "no error!!!";
$error [] = "Second error!!!";
echo $error [0];
echo $error [1];
---------------------Output----------------------------------------
No error!!! Second error!!!
---------------------------------------------------------------------
2. Associative array
An associative array (associative array) uses a string index (or key) to access values stored in an array
An array of associated indexes is useful for database-tier interaction
$newarray = Array ("=>1", "second" =>2, "third" =>3);
echo $newarray ["second"];
$newarray ["Third"]=5;
echo $newarray ["Third"];
---------------------Output----------------------------------------
25
---------------------------------------------------------------------
3. Heterogeneous arrays
PHP arrays can contain different kinds of values
$mixedBag = Array ("Cat", 42,8.5,false);
Var_dump (Mixedbag);
4. Multidimensional arrays
An array containing arrays is created, and array dimensions are not limited, but it is generally difficult to imagine the use of an array with more than three dimensions
$planets = Array (Array ("MM", 1,2), Array ("NN", 3,4), Array ("BB", 5,6), Array ("VV", 7,8));
Print $planets [2][0];
$planets 2 = 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 $planets 2["VV" ["LL"][0];
---------------------Output----------------------------------------
Bbone
---------------------------------------------------------------------
5. Using a circular access array
There are many ways to traverse an array (using a while,for loop) or a foreach statement, and the easiest is a foreach statement
$length = array (0,107,202,400,475);
Converts centimeters to inches
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----------------------------------------
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 over the value of an associative array
$sound = Array ("Cow" => "moo", "Dog" => "woof",
"Pig" => "Oink", "Duck" => "quack");
foreach ($sound as $animal => $noice)
{
echo "$animal scream is such $noice $noice ...";
}
---------------------Output----------------------------------------
Cow's cries are such moo moo ...
Dog's cries are such woof woof ...
Pig's cries are such Oink oink ...
Duck's cries are such quack quack ...
---------------------------------------------------------------------
6. Using Array pointers
Along with the keys and associated values stored in the array, PHP also has an internal index that points to the current element of the array.
There are several functions that use and update the array index to provide access to the elements of a log group
$a = Array ("A", "B", "C", "D", "E", "F");
Echo current ($a);
each ($a);
Key ($a);//current array pointer, return its index
Echo current ($a);//The value of the present element
each ($a);//Returns the value of the current element and points the internal index to the next element
each ($a);
Echo current ($a);
Next ($a);//point to next element
Echo current ($a);
Prev ($a);//point to previous element
Echo current ($a);
End ($a);//point to last element
Echo current ($a);
Key ($a);
Echo current ($a);
---------------------Output----------------------------------------
Abdedff
---------------------------------------------------------------------
7. Basic array functions
Integer count (mixed var)//Returns the number of elements in the array, and can also be used for any variable
Number max (array numbers)//returns the maximum value in an array
Number min (array numbers)//returns the minimum value in an array
Boolean In_array (mixed Needle,array Haystack[,bollean Strict])//Find the value in the array, the third argument is optional, the type check is enforced
Mixed Array_search (mixed Needle,array Haystack[,boolean Strict])//Return key instead of Boolean value,
Returns False when it is not found, returns 0 if the element is exactly the first element, and PHP automatically converts to false so you need to use = = to judge, as follows
$a = Array ("A", "B", "C", "D", "E", "F");
$index = Array_search ("A", $a);
if ($index = = False)
echo "did not find the character ' a ' in array a";
Else
echo "Index = $index";
---------------------Output----------------------------------------
Index = 0
---------------------------------------------------------------------
Array array_reverse (array source[,bool Preserve_keys])//reverse arrays, generating a new array, preserving the association between indexes and elements when the optional argument is true
$a = Array ("A", "B", "C", "D", "E", "F");
$newa = Array_reverse ($a);//Flip directly
echo $newa [0];
$NEWB = Array_reverse ($a, true);//still retains the association between the index and the element
echo $newb [0];
---------------------Output----------------------------------------
Fa
---------------------------------------------------------------------
Sort (array Subject[,integer Sort_flag])//in ascending order of values (rearranging elements in the original array)
Rsort (array Subject[,integer Sort_flag])//descending order by value (rearrange elements in the original array)
Optional parameters can be specified as numeric sort_numeric or as a string sort_string or the usual way sort_regular sort
Sort (), rsort () can be used for associative arrays, but keys will be lost
Asort (array Subject[,integer Sort_flag])//in ascending order of values (rearranging elements in the original array), maintaining key-value associations
Arsort (array Subject[,integer Sort_flag])//descending order by value (reorder elements in the original array), maintaining key-value associations
When Asort () Arsort () is used for unassociated arrays, the elements are rearranged in sorted order, but the index of the access element does not change
Integer ksort (ayyay subject [, integer sort_flag])//in ascending order by key or index
Integer krsort (ayyay subject [, integer sort_flag])//descending order by key or index
Usort (array subject, string compare_function)//by user-defined ordering, the user defines their own collation function, but the function must conform to the
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)//a equals b returns 0
$numbers = Array (16,3,2,171,5,24,6,19);
Sort ($numbers);
foreach ($numbers as $n)
echo $n. " ";
$numbers = Array (16,3,2,171,5,24,6,19);
Rsort ($numbers);
foreach ($numbers as $n)
echo $n. " ";
$numbers = Array (16,3,2,171,5,24,6,19);
Sort ($numbers, sort_string);
foreach ($numbers as $n)
echo $n. " ";
$a = Array ("O" => "KK", "E" => "zz", "Z" => "hh", "a" => "RR");
Asort ($a);
foreach ($a as $keyname => $keyvalue)
Echo $keyvalue;
$a = Array ("O" => "KK", "E" => "zz", "Z" => "hh", "a" => "RR");
Ksort ($a);
foreach ($a as $keyname => $keyvalue)
Echo $keyvalue;
Two strings based on length comparison
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 $an)
Echo $an;
---------------------Output----------------------------------------
2 3 5 6 171 171 6 5 3 2 171 2 3 5 6 Hhkkrrzzrrzzkkhhoxcowmimimonkey
---------------------------------------------------------------------
Array_merge ($a, $b)//combination array, personally considered one of the more interesting functions, from two arrays to generate an array, with the same key value will be overwritten
Code section////////////////////////////////////////
$a = Array ("name" => "Zhangsan", 10,100);
$b = Array ("name" => "Lisi", 4,6,8);
$c = Array_merge ($a, $b);
Var_dump ($c);
//////////////////////////////////////////////////////////////////////////
---------------------Output----------------------------------------
Array (6) {[' Name ']=> string (4) ' Lisi ' [0]=> int (a) [1]=> int (m) [2]=> Int (4) [3]=> int (6) [4]=> Int ( 8)}
---------------------------------------------------------------------
Array Array_combine (array $a, array $b)//a the values of the arrays are the keys to the new array, the value of the B array is the value of the new array, and the array length is not the same, returning false
Code section////////////////////////////////////////
$a = Array ("name", "Math", "" ");
$b = Array ("Zhangsan", 4,6);
$c = Array_combine ($a, $b);
Var_dump ($c);
//////////////////////////////////////////////////////////////////////////
---------------------Output----------------------------------------
Array (3) {[' Name ']=> string (8) ' Zhangsan ' [' Math ']=> int (4) ['] ']=> int (6)}
---------------------------------------------------------------------