1. Direct Assignment method:
① indexed arrays: Storing data by index, memory is not contiguous, but JS memory is contiguous
The code is as follows:
$array [0] = "one"; $array [1] = "A";p rint_r ($array); Prints the array echo "<br>" via Print_r ();
② Associative arrays: Storing data in the form of keys, and memory is not contiguous
The code is as follows:
$array 2["name"] = "Fusaaki"; $array 2["age"] = 18;print_r ($array 2); echo "<br>";
2. defining arrays by array
You can define an array by using the keyword array, and if it is an empty array, you need to add data to the array using the array[] method
The code is as follows:
$array 3 = Array (1,2,3,4, "Demacia");p Rint_r ($array 3), echo "<br>", $array 4 = array (); $array 4[] = 1; Add data to the array by array[] = 2; $array 4[] = 3; $array 4[] = 4;print_r ($array 4); echo "<br>";
3. iterating through an array
① Traversal index array: For loop traversal, but the array in PHP does not have the length property, the count number is required
The code is as follows:
for ($i =0; $i <count ($array 4), $i + +) { //count represents the number of values in the array, there is no array length in PHP, echo $array 4[$i];}
② traversal associative arrays: Use foreach to traverse, and also to traverse associative and indexed functions in a mixed body
The code is as follows:
foreach ($array 2 as $key = + $obj) { echo $key. = ". $obj." <br> ";}
The foreach format at this time is (array name as key = = value);
PHP Learning four: arrays (one)