Home  >  Article  >  Backend Development  >  What are the php array modification functions?

What are the php array modification functions?

coldplay.xixi
coldplay.xixiOriginal
2020-07-23 13:54:252522browse

php array modification functions are: 1. [array_change_key_case] changes all key names in the array to all uppercase or lowercase; 2. [array_fill_keys] fills the array with the specified keys and values; 3. [array_fill] Fills the array with the given values.

What are the php array modification functions?

php array modification functions include:

1, array_change_key_case: Change the keys in the array Change all key names to all uppercase or lowercase. 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.

2, array_fill_keys: Fill the array with the specified keys and values, accepting 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 populate the array.

3, array_fill: Fill 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, it is returned The first index of the array is a negative number, the subsequent indexes start 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.

4, 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 results. 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.

5, 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.

6, 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 , replaces the values ​​in the first array with the values ​​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.

7, 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 replace it recursively. the values ​​in the array.

8, 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, and the second is the starting position. , the third is an optional length, the default is 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.

9, array_unique: Remove duplicate values ​​in the array, accepts two parameters, the first is the array to be deduplicated, and the second is the sort order identifier, PHP5. The default is SORT_REGULAR in 2.9 and SORT_STRING in other versions. 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.

10, 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 keys The name will be recalculated from zero, and the string key name will remain unchanged. Returns the number of inserted array elements.

11, 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 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.

12, array_pop: Pops and returns the last unit of the array, and the array length is reduced by 1. This function only accepts array references and cannot directly pass in the array. 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.

13, array_push: Push one or more units into the end of the array and increase the corresponding length. This function only accepts references 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.

<?php
$ar1=array("a"=>"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));
 
?>

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What are the php array modification functions?. 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