Home > Article > Backend Development > Some introductory notes on learning PHP arrays_PHP tutorial
There are three types of arrays in php: numerical arrays arrays with numeric ID keys associative arrays each ID key in the array is associated with a value multidimensional arrays arrays containing one or more arrays, okay now look at mine Take notes.
The code is as follows | Copy code |
header('Content-Type: text/html; charset=utf-8'); '; <br> function var_array($array) <br> { <br> echo '<pre class="brush:php;toolbar:false">'; <br> var_dump($array); <br> echo ''; } function printr($array) { echo ' '; <br> print_r($array); <br> echo ''; } function getArr($sNum, $eNum=1, $step=1) { $arr = range($sNum, $eNum, $step); $reArr = array(); foreach($arr as $v) { $reArr[$v] = rand(0,10); } unset($arr); return $reArr; } /** * array array exercises */ //--------------------------------------------- //array_change_key_case() changes the uppercase and lowercase letters of the array index, determined by the last parameter: CASE_UPPER (converted to uppercase), CASE_LOWER (default converted to lowercase) $expArr = array( 'fiRsT' => '1', 'sEcoNd' => '2', 'ThIrd' => array( 'HuiMa' => '3', 'nengZhuaNma' => '5', ) ); printr(array_change_key_case($expArr));//Convert all to lowercase printr(array_change_key_case($expArr['ThIrd'], CASE_UPPER));//Convert all to uppercase only convert an index key in the $expArr array //Summary: This function only affects one level of the array. And it will not affect the original array echo ' '; //--------------------------------------------- //array_chunk($array, $size, false) //Split an array into a multi-dimensional array. size determines how the array becomes a multi-dimensional array every $size. true/false determines whether the key values of the new array inherit the keys of the original array $expArr = array('4','2','6','d','2'); printr(array_chunk($expArr, 3)); //Summary: This function only affects one level of the array. And it will not affect the original array echo ' '; //--------------------------------------------- //array_combine($keyArr, $valArr) //Combine two arrays into one array, $keyArr as the key and $valArr as the value $expKey = array('g', 'd', 't'); $expVal = array('5', '8', '7'); printr(array_combine($expKey, $expVal)); //This function also only affects one level of the array and returns a new array echo ' '; //--------------------------------------------- //array_count_values($array) //Count the number of occurrences of each value in the $array array, and use that value as the key of the new array, and the number of occurrences as value $array = array('v1'=>'265', 'v2'=>'352', 'v3'=>'265', 'v4'=>'349', 'v5'=> ;'265'); printr(array_count_values($array)); //Summary: This function can only be used for statistical values of string and integer types. Other types will issue a warning! echo ' '; //--------------------------------------------- //array_diff($array1, $array2...) //Using $array1 as the basic array, its value does not appear in any other parameter array to form a new array $arr1 = array('v4'=>458, 'gren', 'b5', 'a5'); $arr2 = array('v4'=>598, 'red', 'a5', 'c4'); printr(array_diff($arr1, $arr2)); //Summary: Take an array and put it into a bunch of arrays to find the values that are not in the array. Statistics and data comparison should be used //array_intersect($array, $parArr, ....) //This function is functionally the same as array_diff, except that array_intersect() returns shared data, while array_diff is the data that only exists in $array // echo ' '; //--------------------------------------------- //array_diff_assoc($array1, $array2...) //Same as array_diff() function, but this one will also use key for comparison // echo ' '; //--------------------------------------------- //array_diff_key //Same as array_diff() function //It’s just that this only uses the key of $array1 to search with other parameter arrays // echo ' '; //--------------------------------------------- //array_diff_uassoc($arr1, $parArr...., callback function) //The function is the same as array_diff(), but the user needs to define a callback function //I don’t understand the function of this function // echo ' '; //--------------------------------------------- //array_diff_ukey($arr1, $parArr...., callback function) //The function is the same as array_diff_key(), but it is the same as array_diff_uassoc and requires a callback function // // echo ' '; //--------------------------------------------- //array_fill($startInt, $numInt, $value) //Fill $value into a new array. The starting index position of the new array is determined by $startInt. $numInt controls how many indexes are generated by this array. //tip: Except $value, $startInt, $numInt must be numbers, otherwise an error will be reported printr(array_fill(2, 5, 'value')); //Summary: I haven’t thought of what to use it for yet echo ' '; //--------------------------------------------- //array_fill_keys($arrKeys, $value); //The function is the same as array_fill() function. It’s just that $arrKeys [the value of an array] is used as the key of the new array $arrKeys = array('45', 'd', 'b', 'c'); printr(array_fill_keys($arrKeys, 'value')); echo ' '; //--------------------------------------------- //array_filter($arr, callBack callback function) //Filter function, by judging the value of the $arr array, if the callBack callback function returns true, the current key and value will be added to the new array //TIP: The callback function can write a rule to filter out the array keys that do not comply with the rules function cb($val) { return $val%2 == 0; } $array = array('k1'=>3, 'k2'=>5,'k4'=>54654, 'k5'=>8794, 8945, 32549564); printr($array, 'cb'); //tip: It is recommended that the callback function name be enclosed in quotation marks //Summary: This method can be made into an integration of data filtering unset($array); echo ' '; //--------------------------------------------- //array_flip($array) //Convert the relationship between key and value in the array. Only keys of string and integr type are supported. A warning will be issued for other types, and the key value in question will not be converted. In the generated new array, if the keys are the same, it will continuously replace the values of the existing keys $arr = array('k1'=>'v1', 'k2'=>'v2', 'k3'=>'v4', 'k4'=>'v4', 'k5'=> ;'v5'); printr(array_flip($arr)); unset($arr); echo ' '; //--------------------------------------------- //array_key_exists($key, $array) //Determine whether a key exists in the current array and return bool. It can also be used to determine objects $array = array('cb' => 234, 'dv'=>45, 'one'=>897); if(array_key_exists('one', $array)) echo 'Exists in this array'; else echo 'does not exist'; echo ' '; //--------------------------------------------- //array_keys($array, $selSearch_value) //Return the key names in the array and form a new array. If $selSearch_value is specified, then the key name in the array equal to $selSearch_value will be returned $array = getArr(4, 10); printr(array_keys($array)); printr(array_keys($array, '5'));//Search with value unset($array); //Summary: This can also be used for data statistics and data comparison verification echo ' '; //--------------------------------------------- echo 'array_map:'; //array_map('callBack', $array,...) //Return the passed in function to the return value of the callback function //The callback function can also return an array. Moreover, the callback function only accepts the value in an array to be passed in function mapCb($n) { return $n*$n*$n; } $array = getArr(4, 15); printr(array_map('mapCb', $array)); echo ' '; //--------------------------------------------- //array_merge($array,$array2...) //Combine multiple arrays into one array and rewrite the numerical index. $arr1 = getArr(1, 5); $arr2 = getArr(5, 10); printr(array_merge($arr1, $arr2)); //Summary: Combine multiple arrays into a new array. echo ' '; //--------------------------------------------- //array_merge_recursive($arr1, $arr2....) //The function is the same as above. But the function will form a new array with values with the same key name instead of replacing //But if you want to use it, use it according to the actual situation echo ' '; //--------------------------------------------- //array_multisort() //Multi-dimensional array sorting, currently only two-dimensional array sorting is implemented. Three-dimensional estimation cannot be arranged //This function will directly change the order of the member array echo ' '; //--------------------------------------------- //array_pad($arr, $size, $value) //Fill the array. If the current length of $arr is less than $size, then fill the $arr array with $value until the length of $arr is equal to $size //If the length of $arr is greater than or equal to $size, then this function will not fill $arr. If $size is less than 0, it will be filled on the left side of $arr, if it is greater than 0, it will be filled on the right side echo ' '; //--------------------------------------------- //array_pop($array) //Remove the last key of the array. echo ' '; //--------------------------------------------- //array_product($arr) //Return the product of all values in an array. //tip: This function cannot handle non-numeric data. If the incoming array contains ‘strings such as a and b’, then php will report an error $arr = array(4,5,5); echo array_product($arr); echo ' '; //--------------------------------------------- //array_push($arr, $keyArr) //Add $keyArr to the end of the $arr array in the form of key/stack. //The difference between the two functions array_merge() and array_merge_recursive(): // arrap_push() adds a $keyArr to $arr, while the other two functions connect multiple functions into one function echo ' '; //--------------------------------------------- //array_rand($arr, $num=1) //Retrieve the keys in the current array, the number of which is determined by $num, the default is 1 //If $num is 1, then it will return a string //If $num>1 && $num $arr = getArr(5, 15); printr(array_rand($arr, 4)); echo ' '; //--------------------------------------------- //array_reduce() //Similar to array_map(), the values in the array are processed through the callback function and the return value is accepted //This function returns a string. It will calculate all the values in the array and return the calculated value, while array_map calculates the value under each key and returns array //I don’t quite understand, please refer to the manual for examples echo ' '; //--------------------------------------------- //array_replace($array, $parArr,...) //Replace the value of the same key in $array with the value of the key in the parameter array //If the corresponding key is not found in the subsequent parameter array in the $array array, then add it to the end of the new array /*$arr = getArr(4, 10); $arr2 = getArr(6, 15); printr($arr); printr($arr2);*/ $base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), ); $replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry')); printr(array_replace($base, $replacements)); echo ' '; //--------------------------------------------- //array_replace_recursive() recursive replacement //The function is the same as array_replace(). The difference is: array_replace_recursive() can operate on multi-dimensional arrays without changing the structure of $array, while array_replace() will eventually return a one-dimensional array $base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), ); $replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry')); printr(array_replace_recursive($base, $replacements)); echo ' '; //--------------------------------------------- //array_reverse($arr) //Arrange the keys in the array in reverse order echo ' '; //--------------------------------------------- //array_search($value, $array) //Find the key name with the value $value in the $array array //If not found, return false //If there are multiple $values behind the $array array, only the first matching key will be returned //This function is similar to array_keys(), the difference lies in the return value: array_search() will only return a matching key name, while array_keys() can return a one-dimensional array composed of all matching keys echo ' '; //--------------------------------------------- //array_shift($arr) //Remove the first key in the current $arr array, and rearrange the subsequent numeric indexes (but do not change the original order), and the non-numeric indexes remain unchanged. //This function is similar to array_pop(), the difference is that array_pop() removes the last one, and array_shift() removes the head echo ' '; //--------------------------------------------- //array_slice($arr, $offset, $length=0, false) array interception //Return a total of $length elements/keys starting from $offset in the current $arr array, and form a new array to return //If $offset or $length is a negative number, then the offset is in the opposite direction //It feels similar to substring() string interception //Use the examples in the php manual directly $input = array("a", "b", "c", "d", "e"); $output = array_slice($input, 2); // returns "c", "d", and "e" $output = array_slice($input, -2, 1); // returns "d" $output = array_slice($input, 0, 3); // returns "a", "b", and "c" // note the differences in the array keys printr(array_slice($input, 2, -1)); printr(array_slice($input, 2, -1, true)); echo ' '; //--------------------------------------------- //array_spslice($arr, $offset, $length) //Just the opposite of the array_slice() function, this function removes the elements between $offset and $length unset($arr); echo ' '; //--------------------------------------------- //array_sum($arr) //Sum and accumulate all the values in the $arr array. If they are of non-numeric type, try to convert them, but most of them will be 0 after conversion //This function only affects one level of array, similar to array_product() $arr = array( 45,56, 'a', 'b'=>getArr(1, 2), ); printr($arr); echo 'array_sum($arr)',array_sum($arr); echo ' '; //--------------------------------------------- //array_values($arr) //Extract the values in the $arr array to form a new array $arr = array( 'k1'=>45,'k2'=>56, 'k3'=>'a', 'b'=>getArr(1, 2), ); printr(array_values($arr)); echo ' '; //--------------------------------------------- //array_unique($arr) sorts the array //Deduplicate the $arr array and filter duplicate values. Multiple identical values will only keep the first echo ' '; //--------------------------------------------- //array_walk($arr, callback[callback function], $sel_perfix='') // Send each key under the current array to the callback function for processing //If you add the $sel_perfix parameter, the callback function also needs three parameters to receive, otherwise an error will be reported //This function only affects one layer $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); function test_alter(&$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } printr(array_walk($fruits, 'test_print')); array_walk($fruits, 'test_alter', 'fruit'); echo ' '; //--------------------------------------------- //array_walk_recursive() //The function is similar to array_alk(); but it will recurse each level of array in $arr, and the returned array will not change the structure of the original array echo ' '; //--------------------------------------------- //arsort($arr) //Sort the array according to the array key name, and you can sort the letters. If sorting fails, null will be returned echo ' '; //--------------------------------------------- //asort() //The function is similar to arsort(), the difference is: asort() sorts the values |
Array loop
The code is as follows
|
Copy code
|
||||
foreach ($array as $value) {
} while (list($key) = each($array)) {echo $array[$key]; } |
foreach ($array as $value) {
echo $array[$key];