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];

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
