Basic syntax:
$ array name [key value];
Note: If you write a key value does not exist, it will count the group out of bounds error.
Several important functions of PHP array;
(1) Count: Counts the number of elements in an array
Format:
Count ($arr);
(2) Is_array: Determine whether a variable is an array. is an array, returns the true, otherwise, returns false.
Format:
Is_array ($arr);
(3) Print_r: Display array information
Format: Print_r ($arr);
(4) Var_dump: Display array information
Format: Var_dump ($arr);
(5) Explode: Splits a string, splits another string using one string, and splits the result into an array
Format:
Explode ("Divide by what", string);
Cases:
<?php
$STR = "Beijing Shanghai Tianjin Shenzhen Qingdao Guangzhou";
In actual development, involving the splitting of strings, you can consider
$arr =explode ("", $str); Split the $str with an empty partition, the result is an array, stored in an array $arr
Print_r ($arr);
?>
(6) Sort: for sorting
Format:
Sort ($arr);
Example: Use for and foreach to traverse the same array
<?php
$colors = Array (' Red ', ' blue ', ' green ', ' yellow ');
/*
* Iterate with For loop
for ($i =0; $i <count ($colors); $i + +) {
echo $colors [$i]. ' <br/> ';
} */
Traversing with foreach
foreach ($colors as $key => $var) {
Echo $var. "<br/>";
}
?>
By using unset to delete one of the key-value pairs of an array, the array will not rebuild the index.
Example: Deleting an array element
<?php
$arr [0]=123;
$arr [1]=456;
$arr [2]= "Hello";
echo "<br/> before deletion";
echo $arr [1];
Unset ($arr [1]);//delete $arr[1] Element
echo "<br/> deleted after <br/>";
echo $arr [1];
Print_r ($arr);
?>