$array = Array ();
$array ["key"] = "values";
?>
There are two main ways to declare an array in PHP:
1. Declare an array with the array () function,
2. Assign a value directly to the array element.
Array arrays
$users = Array (' phone ', ' computer ', ' dos ', ' Linux ');
echo $users;//Only print out data type array
Print_r ($users);//array ([0] = + phone [1] = computer [2] = = DOS [3] = Linux)
$numbers = range (1,5);//Create an array containing the specified range
Print_r ($numbers);//array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5)
Print_r (true);//1
Var_dump (false);//bool (false)
Print_r can simply print the string and the number, the array will begin with an array and is represented by the key value, Print_r output Boolean and null results are meaningless, so it is more appropriate to use Var_dump
To display all the values in the array through a loop
for ($i = 0; $i < 5; $i + +) {
echo $users [$i];
Echo '
‘;
}
Count the number of cells in an array or the number of attributes in an object by count/sizeof
for ($i = 0; $i < count ($users); $i + +) {
echo $users [$i];
Echo '
‘;
}
You can also iterate through the array through the Foreach loop, which is the benefit of not having to consider key
foreach ($users as $value) {
echo $value. '
';//dot number is a string join symbol
}
The Foreach loop iterates through the $key and $value; $key and $value are variable names that can be set by themselves
foreach ($users as $key = = $value) {
echo $key. '
';//Output key
}
?>
go see what you know? " color code Daquan
PHP Create definition array