Php array function Learning Record 2

Source: Internet
Author: User
Php array function Learning Record 2 1. check whether the given key name or index exists in the array -- array_key_exists

Usage: array_key_exists ($ key (mixed), $ input (array) returns TRUE and FALSE

$input_array=array("1"=>"java","op"=>"R","act"=>"python","php","redis");
$array=array("sd","wd","gh");
var_dump(array_key_exists(1,$array));
var_dump(array_key_exists("op",$input_array));

Result: bool (true)

2. return all key names in the array -- array_keys

Usage: array_keys ($ input (array) returns the key name in the array.

$input_array=array("1"=>"java","op"=>"R","act"=>"python","php","redis");
$array=array("sd","wd","gh");
var_dump(array_keys($array));
var_dump(array_keys($input_array));

Result: array (3) {[0] => int (0) [1] => int (1) [2] => int (2 )}

Array (5) {[0] => int (1) [1] => string (2) "op" [2] => string (3) "act" [3] => int (2) [4] => int (3 )}

3. apply the callback function to the unit of the given array. array_map

Usage: array_map (function, array1 (array), array2. ..) feels that this is a bit similar to the filter function array_filter. array_map can be understood as a processing function and written by itself.

function cube($n){
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
var_dump(array_map("cube", $a));

Result: array (5) {[0] => int (1) [1] => int (8) [2] => int (27) [3] = & gt; int (64) [4] = & gt; int (125 )}

4. merge one or more arrays-array_merge (the union set)

Syntax: append the values in an array to the end of another array. if it is a digital index, the key name will be re-indexed continuously.

$input_array=array("1"=>"java","op"=>"R","act"=>"python","php","redis");
$array=array("sd","wd","gh");
var_dump(array_merge($input_array,$array));

Result: array (8) {[0] => string (4) "java" ["op"] => string (1) "R" ["act"] => string (6) "python" [1] => string (3) "php" [2] => string (5) "redis" [3] => string (2) "sd" [4] => string (2) "wd" [5] => string (2) "gh "}

After the key value is re-indexed, the java key name is changed from 1 to 0.

5. recursively merge one or more arrays -- array_merge_recursive

Usage: like array_merge, only array_merge is the union set, while array_merge_recursive is actually 1 + 1 = 2. duplicate key values will not be overwritten, and duplicate key names will be automatically merged.

$ar1 = array("color" => array("favorite" => "red"), 5,10);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
var_dump(array_merge_recursive($ar1, $ar2));

Result: array (4) {["color"] => array (2) {["favorite"] => array (2) {[0] => string (3) "red" [1] => string (5) "green"} [0] => string (4) "blue"} [0] => int (5) [1] => int (10) [2] => int (10 )}

6. sort multiple arrays or multi-dimensional arrays-array_multisort

Usage: array_multisort (array1, array2. ..).TRUE, Or return when the failure occurs.FALSE.

$ar1 = array("10", 101, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);

Result:

Array (4) {[0] => string (2) "10" [1] => string (1) "a" [2] => int (100) [3] => int (101)} array (4) {[0] => int (1) [1] => int (1) [2] => string (1) "2" [3] => int (3 )}

7. fill the array with a value to the specified length -- array_pad

Usage: array_pad ($ input (array), $ size (int), mixed) returns the filling result.

$array1=array(10,20,30);
$array2=array('java'=>array('redis','mysql'));
var_dump(array_pad($array1,6,'40'));
var_dump(array_pad($array2,4,'shell'));

Result:

Array (6) {[0] => int (10) [1] => int (20) [2] => int (30) [3] => string (2) "40" [4] => string (2) "40" [5] => string (2) "40 "}

Array (4) {["java"] => array (2) {[0] => string (5) "redis" [1] => string (5) "mysql"} [0] => string (5) "shell" [1] => string (5) "shell" [2] => string (5) "shell "}

8. pop up the last unit of the array (Out Stack) -- array_pop

Usage: array_pop ($ input (array) returns the last unit of the array. if array is empty (or not an array ),NULL.

$array1=array(10,20,30);
$array2=array();
var_dump(array_pop($array1));
var_dump(count($array1));
var_dump(array_pop($array2));

Result: int (30) int (2) (array length minus 1) NULL

9. calculate the product of all values in the array -- array_product

Usage: array_product ($ array), which must contain numbers in the array)

$array1=array(10,20,30);
$array2=array(1,'er');
$array3=array(10,20,30=>20);
var_dump(array_product($array1));
var_dump(array_product($array2));
var_dump(array_product($array3));

Result: int (6000) int (0) int (4000)

10. press one or more units to the end of the array (into the stack) -- array_push

Usage: array_push ($ array, mixed...) is appended to the end of the array_pop stack one by one, and returns the number of arrays.

$array1=array(10,20,30);
var_dump(array_push($array1,'erer','weert'));
var_dump($array1);

Result: int (5) array (5) {[0] => int (10) [1] => int (20) [2] => int (30) [3] => string (4) "erer" [4] => string (5) "weert "}

11. randomly retrieve one or more units from the array-array_rand

Usage: array_rand ($ array, $ num_rep (int) randomly retrieves multiple units from the array (the key name is returned)

$array1=array(10,20,30,40,50,60);
var_dump(array_rand($array1,3));

Result: array (3) {[0] => int (0) [1] => int (1) [2] => int (4 )}

12. use the callback function to iteratively simplify the array to a single value-array_reduce

Usage: array_reduce ($ input (array), function, int) iterates the callback function into every unit in the input array, thus simplifying the array into a single value. If the optional parameter initial is specified, the parameter is treated as the first value in the array, or if the array is empty, it is returned as the final value.

function rsum($v, $w){
$v += $w;
return $v;}
function rmul($v, $w){
$v *= $w;
return $v;}
$a = array(1, 2, 3, 4, 5);
$x = array();
var_dump(array_reduce($a, "rsum"));
var_dump(array_reduce($a, "rmul", 10));
var_dump(\array_reduce($x, "rsum", 1));

Result: int (15) int (1200) int (1)

13. use the passed array to recursively replace the element of the first array-array_replace_recursive

Usage: array_replace_recursive (array1, array2, array3. ..) replaces the value of the first array with the value of the following array element. If a key exists in the first array and the second array, its value will be replaced by the value in the second array. If a key exists in the second array but does not exist in the first array, this element is created in the first array. If a key only exists in the first array, it remains unchanged. If multiple replacement arrays are passed, they are processed in order, and the subsequent arrays overwrite the previous values.

14. replace the element of the first array with the passed array -- array_replace

Usage: The array_replace (array1, array2,...) function replaces the value of the first array with the values of the following array elements. If a key exists in the first array and the second array, its value will be replaced by the value in the second array. If a key exists in the second array but does not exist in the first array, this element is created in the first array. If a key only exists in the first array, it remains unchanged. If multiple replacement arrays are passed, they are processed in order, and the subsequent arrays overwrite the previous values.

$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
$basket = array_replace_recursive($base, $replacements);
print_r($basket);
$basket = array_replace($base, $replacements);
print_r($basket);

Result: Array ([citrus] => Array ([0] => pineapple) [berries] => Array ([0] => blueberry [1] => raspberry ))

Array ([citrus] => Array ([0] => pineapple) [berries] => Array ([0] => blueberry ))

15. return an array with the opposite unit order -- array_reverse

Usage: array_reverse ($ array, <参数> ) TRUE indicates that the original key name is retained.

$input  = array("php", 4.0, array("green", "red"),'a'=>'b');
var_dump(array_reverse($input));
var_dump(array_reverse($input, TRUE));

Result: array (4) {["a"] => string (1) "B" [0] => array (2) {[0] => string (5) "green" [1] => string (3) "red"} [1] => float (4) [2] => string (3) "php "}

Array (4) {["a"] => string (1) "B" [2] => array (2) {[0] => string (5) "green" [1] => string (3) "red"} [1] => float (4) [0] => string (3) "php "}

16. search for a given value in the array. if the value is successful, the corresponding key name -- array_search is returned. if the value fails, FALSE is returned.

Usage: array_search (mixed, $ array)

$input  = array(1=>"php", 2=>'java',3=>'mysql','a'=>'b');
var_dump(array_search('php',$input));
var_dump(array_search('b',$input));

Result: int (1) string (1) ""

17. remove the elements starting with the array from the array -- array_shift is similar to a pointer.

Usage: array_shift (array); removes the first unit of array and returns the result. This removes the length of array and moves all other units one bit forward. The names of all numeric keys are counted from scratch, and the names of text keys remain unchanged. If array is null (or not an array ),NULL

$input  = array(1=>"php", 2=>'java',3=>'mysql','a'=>'b');
var_dump(array_shift($input));
var_dump($input);

Result: string (3) "php"

Array (3) {[0] => string (4) "java" [1] => string (5) "mysql" ["a"] => string (1) "B "}

18. extract a segment from the array -- array_slice

Usage: array_slice (array, offset (int), length (int ), ). If the parameter TRUE is added, the key of the array will not be duplicated.

$input = array("a", "b", "c", "d", "e");
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));

Result: Array ([0] => c [1] => d) Array ([2] => c [3] => d)

19. remove part of the array and replace it with other values -- array_splice

Array_splice ($ input (array), $ offset (int), $ length (int), $ replace (array ))

Remove the units specified by offset and length from the input array. if The replacement parameter is provided, replacement is used. Returns an array containing the removed cells. Note that the number key name in input is not retained. If the offset value is positive, it is removed from the offset specified by this value in the input array. If offset is negative, it is removed from the offset specified by the reciprocal value at the end of input.

If length is omitted, all parts from the offset to the end of the array are removed. If length is specified and it is a positive value, so many units are removed. If length is specified and it is a negative value, all units from offset to the end of the array are removed. Tips: when replacement is given, count ($ input) is used as the length to remove all units from the offset to the end of the array.

If the replacement array is provided, the removed unit is replaced by the unit in the array. If the combination of offset and length does not remove any value, the unit in the replacement array will be inserted to the position specified by offset. Note that the key names in the replace array are not retained. If the value to be replaced is only a unit, you do not need to add array () to it unless the unit itself is an array.

 $input1 = array("red", "green", "blue", "yellow");
array_splice($input1, 2);
var_dump($input1);
$input2 = array("red", "green", "blue", "yellow");
array_splice($input2, 1, -1);
var_dump($input2);
$input3 = array("red", "green", "blue", "yellow");
array_splice($input3, 1, count($input3), "orange");
var_dump($input3);
$input4 = array("red", "green", "blue", "yellow");
array_splice($input4, -1, 1, array("black", "maroon"));
var_dump($input4);
$input5 = array("red", "green", "blue", "yellow");
array_splice($input5, 3, 0, "purple");
var_dump($input5);

Array (2) {[0] => string (3) "red" [1] => string (5) "green "}

Array (2) {[0] => string (3) "red" [1] => string (6) "yellow "}

Array (2) {[0] => string (3) "red" [1] => string (6) "orange "}

Array (5) {[0] => string (3) "red" [1] => string (5) "green" [2] => string (4) "blue" [3] => string (5) "black" [4] => string (6) "maroon "}

Array (5) {[0] => string (3) "red" [1] => string (5) "green" [2] => string (4) "blue" [3] => string (6) "purple" [4] => string (6) "yellow "}

20. calculate the sum of all values in the array -- array_sum

Usage: array_num (array) (number)

 $a = array(2, 4, 6, 8);
$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
var_dump(array_sum($a));
var_dump(array_sum($b));

Result: int (20) float (6.9)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.