PHP array study notes

黄舟
Release: 2023-03-03 14:40:01
Original
1047 people have browsed it

. The code is as follows:


header('Content-Type: text/html; charset=utf-8');
echo '

'; <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 () Change the uppercase and lowercase letters of the array index, determined by the last parameter: CASE_UPPER (convert to uppercase), CASE_LOWER (default 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 will not affect the original array
echo '


';
//---------------- -----------------------------
//array_chunk($array, $size, false)
//Split an array into A multi-dimensional array, size determines how this array becomes a multi-dimensional array every $size, true/false determines whether the key values ​​of the new array will 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 will not affect the original array
echo '


';
//---------------- -----------------------------
//array_combine($keyArr, $valArr)
//Combine two arrays into one Array, $keyArr as key, $valArr as 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 its 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 base array, its value does not appear The values ​​in any other parameter array 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 It is 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
//This just 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 requires the user 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(), except that like array_diff_uassoc, a callback function is required
//
//
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: In addition to $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
echo '< ;br/>

';
//-------------------------- ------------------
//array_fill_keys($arrKeys, $value);
//The function is the same as array_fill() function. But here $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 pairing the $arr array Judgment of the value, 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 quotes
//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 judge 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, data comparison and verification
echo '< br/>

';
//----------------------------- ----------------
echo 'array_map:';
//array_map('callBack', $array,...)
//Return the passed in function 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 numeric 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 them
//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 the 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 array the last key.
echo '


';
//------------------------ ---------------------
//array_product($arr)
//Returns the product of all values ​​in an array.
//tip: This function cannot handle non-numeric data. If the incoming array contains 'a, b and other strings', 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 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//Otherwise php will report an error
$arr = getArr (5, 15);
printr(array_rand($arr, 4));
echo '


';
//------ ---------------------------------------
//array_reduce()
//and array_map () is similar, through the callback function, the values ​​in the array are processed 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 will calculate the value under each key and return the array
//Not too clear, see the manual for examples
echo '< br/>

';
//-------------------------------- ----------------
//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 '

< br/>';
//---------------------------------------- ----
//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 one will be returned matching keys
//This function is similar to array_keys(), the difference lies in the return value: array_search() will only return one matching key name, while array_keys() can return a one-dimensional array consisting of all matching keys Array
echo '


';
//----------------------- -----------------------
//array_shift($arr)
//Remove the first key in the current $arr array, and shift the following numbers The indexes are rearranged (but the original order is not changed), and the non-numeric indexes remain unchanged.
//This function is similar to array_pop(), the difference is that array_pop() removes the last one, array_shift() removes the head
echo '


';
//------------------------------------------------
// array_slice($arr, $offset, $length=0, false) Array interception
//Returns the offset starting from $offset in the current $arr array, a total of $length elements/keys, and forms 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
//Just use the example in the PHP manual
$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 If it is a non-numeric type, try to convert it, but most of the conversions will be 0.
//This function will only affect one layer of arrays, 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) Deduplicate the array
//Deduplicate the $arr array and filter duplicate values. Multiple identical values ​​will only retain the first one
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()
//Function is similar to array_alk(); but it will be recursive$ For each level of array of arr, 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 array alphabetically. If the sorting fails, null will be returned
echo '


';
//----------------- ----------------------------
//asort()
//The function is similar to arsort(), the difference is: asort() It is to sort the values ​​

The above is the content of the PHP array study notes. For more related articles, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!