Indexed array: An array with a numeric index
Initializing an array
$arr Array (' value1 ', ' value2 ', ' value3 '); $arr = [' value1 ', ' value2 ', ' value3 ']; /* PHP5.4 version starts to support */
Iterating through an array
For loop traversal:
for ($i$i<count($arr$i+ +) {echo$ Arr[$i]. ' ';}
foreach Loop traversal:
foreach ($arras$value) { echo$value. ' ';}
Array sorting
Sort ($arr) /* sort the indexed array in ascending order */ Rsort ($arr) /* sort the indexed array in descending order */ /* when these two functions are used for associative arrays, the key values are ignored to sort the associative array into an indexed array */
Associative arrays: Arrays of custom key values
Initializing an array
$arr Array (' key1 ' = ' value1 ', ' key2 ' = ' value2 ', ' key3 ' = ' value3 '); $arr = [' key1 ' = ' value1 ', ' key2 ' = ' value2 ', ' Key3 ' and ' value3 ']; /* PHP5.4 version starts to support */
Iterating through an array
An indexed array cannot be traversed with a for loop (no numeric index exists), but it can still be used with foreach:
foreach ($arras$key= +$value) { echo$key. ' = '. $value. " ;}
There is also a cool way to traverse:
while (list($key,$valueeach ($arr)) { echo$key . ' = '. $value. " ;}
Array sorting
Asort($arr);/*sorts in ascending order based on the values of the array. */Ksort($arr);/*Sort Ascending by the keys of the array. */Arsort($arr);/*sorts in descending order based on the values of the array. */Krsort($arr);/*sorts the keys of the array in descending order. */ /*Ksort (), Aksort () Two functions are not valid for indexed arrays, Asort (), Arsort () two functions are equivalent to sort (), Rsort () for an indexed array*/
Array Common functions
Count();/*returns the array length (number of elements)*/ Array_pop();/*Delete the last element of the array (out of the stack)*/ Array_push();/*inserts one or more elements into the end of an array (into the stack)*/Array_merge();/*combine one or more arrays into an array*/Array_unique();/*Remove the same element from the array*/In_array();/*detects if a value is in an array (returns True and false)*/Shuffle();/*disturb the original array*/...
PHP Basics Summary--arrays