Home  >  Article  >  Backend Development  >  Study notes of php array_PHP tutorial

Study notes of php array_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:40666browse

If you are not technically proficient, please point out if there are any mistakes and I will correct them.

Copy code 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() 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 and 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 the array becomes a multi-dimensional array every $size, true/false determines whether the key value of the new array inherits the key value 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 the key, $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 this 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 type values, other types will issue a warning!
echo '


';
//-------------------------- ------------------------
//array_diff($array1, $array2...)
//Using $array1 as The basic array, whose value does not appear in any other parameter array, forms 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 use
//array_intersect($array, $parArr, ....)
//This function It has the same function as array_diff, except that array_intersect() returns common 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
//This one only takes $ The key of array1 is searched 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 '< ;br/>

';
//-------------------------- ------------------
//array_diff_ukey($arr1, $parArr...., callback function)
//The function is the same as array_diff_key(), It's just like array_diff_uassoc, it needs 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: In addition to $value, $startInt, $numInt must be numbers, otherwise an error will be reported
printr(array_fill(2, 5, 'value'));
//Summary: Haven’t thought of doing it yet What is the use of
echo '


';
//------------------ -----------------------------
//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 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 '
< ;hr/>
';
//-------------------------------- -------------
//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, returns 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 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 function passed in and return the return value of the callback function
//Callback Functions can also return an array. Moreover, the callback function only accepts the value in an array passed into
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: More arrays to form 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
echo '

< ;br/>';
//----------------------------------------- -------
//array_multisort()
//Multi-dimensional array sorting, currently only two-dimensional array sorting is implemented. Three-dimensional estimation cannot be sorted
//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 use $value to fill the $arr array 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)
//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 to Add 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 A function
echo '


';
//------------------ -----------------------------
//array_rand($arr, $num=1)
//Get the current array The number of keys taken out is determined by $num, the default is 1
//If $num is 1, then it will return a string
//If $num>1 && $num//Otherwise php reports an error
$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 the array
//Not too clear, see the manual for examples
echo '


';
//---------------------- -----------------------
//array_replace($array, $parArr,...)
//Use the key in the parameter array The value replaces the value of the same key in $array
//If the corresponding key is not found in the subsequent parameter array in the $array array, then add it to the back 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 the $array array has more than one $value, then 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 Returns a one-dimensional array consisting 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, 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/ key and form a new array to return
//If $offset or $length is a negative number, then it is offset in the opposite direction
//It feels similar to substring() string interception
//Direct 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 '< ;br/>

';
//-------------------------- ------------------
//array_sum($arr)
//Sum and accumulate all the values ​​in the $arr array, if it is a non-numeric value Types are tried to be converted, but most of them are 0 after conversion
//This function only affects 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 of
$arr = array(
'k1'=>45,'k2'=>56, 'k3'=>'a', 'b'=>getArr(1, 2 ),
);
printr(array_values($arr));
echo '


';
//- -----------------------------------------------
//array_unique ($arr) Array array
//Array $arr array and filter duplicate values. Multiple identical values ​​will only retain 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 the $sel_perfix parameter is added, 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 original Array structure
echo '


';
//----------------- ----------------------------
//arsort($arr)
//Sort the array according to the array key name, Can be sorted alphabetically. If the sorting fails, null will be returned
echo '


';
//------------- --------------------------------
//asort()
//Function is similar to arsort() ), the difference is: asort() sorts the values ​​

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325462.htmlTechArticleI am not technically proficient. If there are any mistakes, please point them out and I will correct them. Copy the code as follows: header('Content-Type: text/html; charset=utf-8'); echo 'pre'; function var_array($array) { ec...
Statement:
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