Home  >  Article  >  Backend Development  >  php function to modify array elements

php function to modify array elements

藏色散人
藏色散人Original
2019-09-18 09:51:506707browse

php function to modify array elements

php function to modify array elements

array_change_key_case: Modify all key names in the array to all uppercase or lower case. Accepts two parameters, the first is the array to be modified, and the second is the optional case conversion flag, indicating whether to modify the key name to uppercase or lowercase, the default is CASE_LOWER. If the input is not an array, false will be returned and a warning will be generated.

array_fill_keys: Fills the array with the specified keys and values, accepts two parameters, the first is the specified key array, the value of the array is the key name, and the second parameter is the value used to fill the array.

array_fill: fills the array with the given value, accepts three parameters, the first is a positive integer, identifying the starting index value of filling, if it is a negative number, the first index of the returned array is Negative number, the following index starts from 0, and the second one is an integer, indicating the amount of filling, which must be greater than or equal to 0, otherwise a warning will be generated. The third parameter is the value used to fill the array.

array_flip: Exchange the keys and values ​​in the array. If the value in the array is not a legal key name, a warning will be generated, and the problematic key-value pair will not appear in the result. If the same value appears multiple times, the last key name that appears will be used as the exchanged value, and the previous one will be discarded. Returns null if the exchange fails.

array_pad: Fill a value into the array with the specified length. Accepts three parameters, the first is the array to be filled, the second is the size of the array after filling, and the third is the value used for filling. If the specified array size is an integer, it will be filled from the right. If it is a negative number, it will be filled from the left. If it is smaller than the size of the original array, it will not be filled. Up to 1048576 values ​​can be filled at once. What is returned is a copy of the first array.

array_replace: Replace the elements of the first array with the passed array, accepts any number of arrays, if a key exists in the first array and also exists in the second array, the first array is replaced with the value in the second array. If it does not exist in the first array but exists in the second array, the element will be created in the first array. If it only exists in the first array it will be left unchanged. If multiple arrays are passed, It will be processed in order, and the subsequent array will overwrite the previous values ​​of the same key. If an error occurs, null is returned, otherwise the replaced array is returned. array_replace is non-recursive and does not determine the type of the value in the first array but overwrites it directly.

array_replace_recursive: The only difference from array_replace is that array_replace_recursive is recursive, that is, it will determine the type of the value in the first array. If it is an array, it will recursively replace the value in the array.

array_splice: Remove a certain part of the array and replace it with other values. It accepts four parameters. The first is the array reference to be operated on, the second is the starting position, and the third is optional. Length, defaults to the length of the array. The fourth is an optional replacement unit, which defaults to an empty array. Key names in the array being operated on are not preserved. If the starting position is a positive number, it is calculated from front to back, starting from 0. If it is a negative number, it is calculated from back to front, starting from -1. If the length is not passed in, it defaults to all units from the starting position to the end of the array. If the length is a positive number, the specified length of units is removed from the starting position. If it is a negative number, it is moved forward from the starting position. Removes cells of the specified length. If 0, no cells are removed. If the replacement unit is an array, the removed unit is replaced with the unit in the array. If no unit is removed, the replacement unit is inserted at the specified starting position. If the replacement unit has only one unit, there is no need to add array(), unless the unit itself is an array, object, or null, and the return value is an array containing the deleted unit.

array_unique: remove duplicate values ​​from the array, accepts two parameters, the first is the array to be deduplicated, the second is the sort order identifier, PHP5.2.9 defaults to SORT_REGULAR, other versions default is SORT_STRING. First sort the element values ​​in the array, and then only retain the first key name encountered for each value, ignoring subsequent key names. It does not mean that the first key name of the same value before sorting will be retained. . Return the deduplicated array, retaining key names.

array_unshift: Insert one or more units at the beginning of the array. The units are inserted as a whole. The incoming units will maintain the same order. After insertion, all numerical key names will be recalculated from zero. Characters The string key name remains unchanged. Returns the number of inserted array elements.

array_shift: Move the unit at the beginning of the array out of the array, move all units forward one bit, all numeric key names start counting from zero, the text key names remain unchanged, and the array length is reduced by 1. Using this function will reset the pointer inside the array. A warning will be generated if a non-array value is passed in. If an empty array or illegal value is passed in, null will be returned.

array_pop: Pops and returns the last unit of the array, and the array length is reduced by 1. This function only accepts a reference to the array and cannot directly pass it in. If it is an empty array, null is returned. A warning will be generated if a non-array value is passed in. Using this function will reset the pointer inside the array.

array_push: Push one or more units into the end of the array and increase the corresponding length. This function only accepts a reference to the array and cannot be passed directly into the array. A warning will be generated if a non-array value is passed in. The pointer inside the array will not be reset after using this function. The return value is the total number of cells in the array after insertion.

"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e","f"=>"f");
$ar2=array("a","b","d","f","g","h");
$ar3=array("a","c","g");
$ar4=array("a"=>"a","b"=>array("a"=>"a","b"=>"b","c"=>"c"),"c"=>"c");
$ar5=array("a"=>"1","b"=>array("c"=>"c","d"=>"d","b"=>"b","e"=>"e"),"c"=>array("c","b","a"));
$ar6=array(1,2,3,4,5,6);
$ar7=array("a","b","d","f","g","h","a","c","g","e");
$ar8=array(1=>"a","02"=>"b",3=>"e",4=>"04");
$ar9=array();
 
var_dump(array_change_key_case($ar1,CASE_UPPER));
var_dump(array_fill_keys($ar6,"a"));
var_dump(array_fill_keys($ar6,$ar3));
var_dump(array_fill(-2,3,"a"));
var_dump(array_fill(3,0,"a"));
var_dump(array_flip($ar2));
var_dump(array_pad($ar2,"-10","z"));
var_dump(array_replace($ar4,$ar5));
var_dump(array_replace_recursive($ar4,$ar5));
var_dump(array_splice($ar3,1,1,array("x","y","z")));
var_dump($ar3);
var_dump(array_unique($ar7));
var_dump(array_unshift($ar8,"e","f"));
var_dump($ar8);
next($ar8);
var_dump(key($ar8));
var_dump(array_shift($ar8));
var_dump($ar8);
var_dump(key($ar8));
var_dump(array_shift($ar9));
next($ar8);
var_dump(key($ar8));
var_dump(array_pop($ar1));
var_dump(key($ar8));
var_dump($ar8);
var_dump(array_pop($ar9));
next($ar8);
var_dump(key($ar8));
$ar1[]="e";
var_dump($ar8);
var_dump(key($ar8));
var_dump(array_push($ar8,"f","g"));
var_dump(key($ar8));
 
?>

For more PHP knowledge, please visit PHP tutorial!

The above is the detailed content of php function to modify array elements. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:What file type is php?Next article:What file type is php?