Php array traversal method and details (note) // traverse the array
$ N = array (5, 7, 9, 1, 6, 2, 3 );
$ Sum = 0;
Echo"
Total ". count ($ n)." count
"; // Calculate the number of elements in the array
For ($ I = 0; $ I
{
Echo $ n [$ I];
$ Sum = $ sum + $ n [$ I];
}
Echo"
The total number is ". $ sum;
/*************************************** *************************/
// When an array is created, no subscript is specified for an element. php will automatically add a subscript (keyword) as the element with the largest subscript (integer)
$ Arr = array (5 => "logo", 567,580 );
Echo"
". $ Arr [5]; // The output value is the logo.
// $ Arr [5] = "yes"; replace the value of an element
Echo"
". $ Arr [6]; // The output value is 567.
Echo"
". $ Arr [7]."
"; // The output value is 580.
/*************************************** *************************/
$ Ar [true] = "hello"; // = $ ar [1]
$ Ar [false] = "word"; // = $ ar [0]
$ Ar [null] = "Hefei"; // = $ ar [""] null in php in many cases <=> ""
/*************************************** *************************/
// We can usually use print_r to display this array. var_dump provides more comprehensive information.
"
". Print_r ($ arr );
Echo"
";
"
". Var_dump ($ arr );
/*************************************** *************************/
$ Arra = array (2, 3 );
// Php arrays can be dynamically increased
$ Arra [2] = 56;
Echo"
". $ Arra [2];
/******************* Several important functions in php array ************ *********/
Echo"
The array length is ". count ($ n); // counts the number of arrays.
Echo"
";
Echo is_array ($ n); // determines whether the given variable is an array. if the returned value is 1, the others are false.
Echo"
";
Print_r ($ n); // display the array
Echo"
";
Var_dump ($ n); // display the array, including data types that are more comprehensive than print_r
Echo"
";
$ Str = "Shushan Baohe Yaohai Kaikai ";
$ S = explode ("", $ str); // used to split ("") and who ("$ str, after splitting, $ s is already an array.
Print_r ($ s );
Echo"
";
Echo is_array ($ s); // $ s is an array.
/******************* Array traversal method *************** ****************/
$ Colors = array ('red', 'blue', 'black', 'green', 'yellow ');
For ($ I = 0; $ I
{
Echo"
$ Colors [$ I] ";
}
Echo"
";
$ I = 0;
While ($ I
{
Echo"
$ Colors [$ I] ";
$ I ++;
}
Echo"
";
Foreach ($ colors as $ k => $ v)
{
Echo"
$ K = $ v ";
}
// Unset ($ colors [2]); delete array $ colors subscript 2 element subscript 2 after deletion the subscript will not move up to fill the gap