Summary of array processing functions in PHP

Source: Internet
Author: User
? Php # change the case sensitivity of the array key $ arr1array (aLamp, dbdatabase, LANGUAGEPHP); print_r (array_change_key_case ($ arr1, CASE_UPPER); echobr; print_r (array_change_key_case, CASE_LOWER); echohrbr; # split an array into multiple third parameters and set whether to retain the key

? Php # change the case sensitivity of the array key $ arr1 = array (a = Lamp, db = database, LANGUAGE = PHP); print_r (array_change_key_case ($ arr1, CASE_UPPER); echo br /; print_r (array_change_key_case ($ arr1, CASE_LOWER); echo hrbr/; # split an array into multiple third parameters to set whether to retain the key

# Changing the case sensitivity of the array key
$ Arr1 = array ("a" => "Lamp", "db" => "database", "LANGUAGE" => "PHP ");
Print_r (array_change_key_case ($ arr1, CASE_UPPER ));
Echo"
";
Print_r (array_change_key_case ($ arr1, CASE_LOWER ));
Echo"
";

# Split an array into multiple third parameters to set whether to retain the key
$ Arr2 = array ('A', 'B', 'C', 'D', 'E', 'F', 'G ');
Print_r (array_chunk ($ arr2, 2, true ));
Echo"
";
Print_r (array_chunk ($ arr2, 2, false ));
Echo"
";

# Array array_diff_assoc (array $ array1, array $ array2 [, array $...]) returns an array,
# This array includes all the values in the array of array1 but not in any other parameter array.
# Different keys

$ Arr3 = array ('A' => 'green', 'B' => 'brown ', 'c' => 'red ');
$ Arr4 = array ('A' => 'green', 'yellow', 'red ');
Print_r (array_diff_assoc ($ arr3, $ arr4 ));
Echo"
";

# Array_diff (array $ array1, array $ array2 [, array $...])
# Returns an array that contains all but not any
# The values in other parameter arrays are not counted if the keys are different.
Print_r (array_diff ($ arr3, $ arr4 ));
Echo"
";

# Array_fill (int $ start_index, int $ num, mixed $ value)
# Use the value of the value parameter to fill an array with num entries,
# The key name is specified by the start_index parameter.
Print_r (array_fill (-5, 8, "banana "));
Echo"
";


# Array_flip (array $ trans)
# Returns an inverted array. For example, the key name in trans is changed to a value,
# The value in trans is the key name.
$ Arr5 = array ('A' => '1', "B" => "2", "c", "d", "e ");
Print_r (array_flip ($ arr5 ));
Echo"
";

# Array_map (callback $ callback, array $ arr1 [, array $...])
# Returns an array containing all the elements in arr1 that have passed the callback function
# Subsequent units. The number of parameters accepted by callback should be passed to array_map ()
# The number of functions is the same.
Function cube ($ n ){
Return $ n * $ n;
}
$ Arr6 = array (1, 2, 3, 4, 5 );
Print_r (array_map ("cube", $ arr6 ));
Echo"
";

# Array_merge_recursive (array $ array1 [, array $...])
# Merge the units of one or more arrays. The values in an array are appended to the previous array.
. Returns an array of results. If the input array has the same string key name,
# Then these values will be merged into an array, which will be recursive, so if a value itself
# Is an array. This function combines it into another array according to the corresponding entries. However, for example
# If the array has the same Array key name, the next value will not overwrite the original value, but will be appended
.
$ Arr7 = array ("color" => array ("favorite" => "red"), 5 );
$ Arr8 = array (10, array ("favorite" => "yellow", "blue "));
Print_r (array_merge_recursive ($ arr7, $ arr8 ));
Echo"
";

# Array_reduce (array $ input, callback $ function [, int $ initial]
# Iterate the callback function to every unit in the input array, so that the array
# Simplified to a single value. If the optional parameter initial is specified, the parameter is treated as
# Process a value, or use the final return value if the array is empty. If the array is empty and no
# If the initial parameter is passed, array_reduce () returns NULL.
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 ();
$ B = array_reduce ($ a, "rsum ");
$ C = array_reduce ($ a, "rmul", 10 );
$ D = array_reduce ($ x, "rsum", 1 );
Echo $ B. "\ t". $ c. "\ t". $ d. "\ n ";
Echo"
";

# Array_replace (array & $ array, array & $ array1 [, array & $ array2 [, array & $...])
# The function replaces the value of the first array with the value of the following array element. If a key exists in the first
# An array also exists in the second array, and its value will be replaced by the value in the second array. If
# If the key exists in the second array but does not exist in the first array, this
# Element. If a key only exists in the first array, it remains unchanged. If multiple replicas are passed
# Groups, which are processed sequentially, and the subsequent arrays overwrite the previous values.

$ Base = array ("orange", "banana", "apple", "raspberry ");
$ Replacements = array (0 => "pineapple", 4 => "cherry ");
$ Replacements2 = array (0 => "grape ");
# Print_r (array_replace ($ base, $ replacements, $ replacements2 ));
# Echo"
";

# Array_splice (array & $ input, int $ offset [, int $ length [, array $ replacement])
# Remove the units specified by offset and length from the input array. If replacement is provided
# Parameter, which is replaced by cells in the replacement array. Returns an array containing the removed cells.
#. Note that the number key name in input is not retained. If length is omitted, remove
# Offset all parts to the end. 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, remove the reciprocal length from offset to the end of the array.
# All units in the middle. TIPS: Remove from offset
# Count ($ input) is used as the length for all cells at the end of the array.

$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input, 1,-1 );
Print_r ($ input );
Echo"
";


# Key (array & $ array)
# Return the key name of the current cell in the array.
$ Fruit = array ("fruit1" => "apple", "fruit2" => "orange", "fruit3" => "grape ",
"Fruit4" => "apple", "fruit5" => "apple ");
While ($ fruit_name = current ($ fruit )){
If ($ fruit_name = 'apple '){
Echo key ($ fruit )."
";
}
Next ($ fruit );
}
Echo"
";
?>

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.