Php array function Learning Record 1

Source: Internet
Author: User
Php array function Learning Record 1. the returned string key names are all lowercase or uppercase arrays. Array_change_key_case

Usage: array_change_key_case ($ input (array), CASE_UPPER | CASE_LOWER), where CASE_LOWER returns lower case (default), and CASE_UPPER returns upper case

Example:

$input_array = array("FirSt" => 1, "SecOnd" => 4);
var_dump(array_change_key_case($input_array, CASE_UPPER));
var_dump(array_change_key_case($input_array));

Result: array (2) {["FIRST"] => int (1) ["SECOND"] => int (4 )}

Array (2) {["first"] => int (1) ["second"] => int (4 )}

2. divide an array into multiple arrays. Array_chunk

Usage: array_chunk ($ input (array), $ size (int), TRUE | FALSE). $ size indicates the number of new arrays. TRUE retains the original key name, if the value is FALSE, the index starts from the numeric index again (default ).

$input_array = array('a'=>'python',"b"=>"java","c"=>"php","d"=>"mysql","e"=>"javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
var_dump(array_chunk($input_array, 2));
var_dump(array_chunk($input_array, 2, true));

Result: array (4) {[0] => array (2) {[0] => string (6) "python" [1] => string (4) "java"} [1] => array (2) {[0] => string (3) "php" [1] => string (5) "mysql"} [2] => array (2) {[0] => string (10) "javascript" [1] => string (5) "redis"} [3] => array (2) {[0] => string (1) "R" [1] => string (7) "mongodb "}}

Array (4) {[0] => array (2) {["a"] => string (6) "python" ["B"] => string (4) "java"} [1] => array (2) {["c"] => string (3) "php" ["d"] => string (5) "mysql"} [2] => array (2) {["e"] => string (10) "javascript" ["f"] => string (5) "redis"} [3] => array (2) {["g"] => string (1) "R" ["h"] => string (7) "mongodb "}}

3. create an array with the value of one array as its key name and the value of the other array as its value array_combine

Usage: array_combine ($ input1 (array), $ input2 (array )). the array in $ input1 is the key name, and the array in $ input2 is the key value. if the length is different, FALSE is returned;

$a=array("a","b","c");
$b=array("php","python","mysql");
$c = array_combine($a, $b);
var_dump($c);
$e=array("a","b");
$f=array_combine($e,$b);
var_dump($f);

Result:

Array (3) {["a"] => string (3) "php" ["B"] => string (6) "python" ["c"] => string (5) "mysql "}
Warning: Array_combine () [function. array-combine]: Both parameters shold have an equal number of elements inD: \ WWW \ array \ arraytest. phpOn line8
Bool (false)

4. count the number of occurrences of all values in the array. array_count_values

Usage: array_count_values ($ input (array ))

$input_array = array('a'=>'python',"b"=>"java","c"=>"php","d"=>"mysql","e"=>"javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
$array = array(1, "hello", 1, "world", "hello");
var_dump(array_count_values ($array));
var_dump(array_count_values ($input_array));

Result:

Array (3) {[1] => int (2) ["hello"] => int (2) ["world"] => int (1 )}

Array (8) {["python"] => int (1) ["java"] => int (1) ["php"] => int (1) ["mysql"] => int (1) ["javascript"] => int (1) ["redis"] => int (1) ["R"] => int (1) ["mongodb"] => int (1 )}

5. calculate the array's difference set-array_diff

Usage: array_diff ($ input1 (array), $ input2 (array), $ input (array)...); returns the value in $ input but not in other arrays.

$input_array = array('python',"b"=>"java","c"=>"php","d"=>"mysql","e"=>"javascript","f"=>"redis","R","h"=>"mongodb");
$array = array("javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
var_dump(array_diff($input_array,$array));

Result: array (4) {[0] => string (6) "python" ["B"] => string (4) "java" ["c"] => string (3) "php" ["d"] => string (5) "mysql "}

6. use the callback function to compare the difference set of the calculated array with the key name-array_diff_ukey

The usage is the same as that of array_diff, except that array_diff is based on the key value. array_diff_ukey is only based on the key name. However, you need to use a callback function for comparison, however, if I look at other online statements or the manual, it seems that all the callback functions look like this. if I have a clear understanding, I can give you some advice and use it directly in the example.

function key_compare_func($key1, $key2){
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));

Result: array (2) {["red"] => int (2) ["purple"] => int (4)} (all result sets are difference sets, this is also the so-called callback function ...)

7. use the callback function provided by the user to perform index check to calculate the array's difference set ----- array_diff_uassoc

Usage: The above usage is the same, but the key name is also compared based on the key value.

function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$array3=array("a" => "green", "red", "b" => "brown", "c" => "blue");
$array4=array("a" => "green", "red", "yellow");
var_dump(array_diff_uassoc($array1, $array2, "key_compare_func"));
var_dump(array_diff_uassoc($array3, $array4, "key_compare_func"));

Result: array (3) {["B"] => string (5) "brown" ["c"] => string (4) "blue" [0] => string (3) "red "}

Array (2) {["B"] => string (5) "brown" ["c"] => string (4) "blue "}

8. use the key name to compare the difference set of the calculated array. array_diff_key

The usage is the same as that of array_diff. this is to calculate the difference set by the key name. More importantly, there is no callback function.

$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_key($array1, $array2));

Result: array (2) {["red"] => int (2) ["purple"] => int (4 )}

9. array_diff_assoc

The usage is the same as that of array_diff and array_diff_key. indexes are added during comparison.

$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "red", "yellow");
$array3 = array ("a" => "green", "yellow", "red");
var_dump(array_diff_assoc($array1, $array2));
var_dump(array_diff_assoc($array1, $array3));

Result: array (2) {["B"] => string (5) "brown" ["c"] => string (4) "blue "}

Array (3) {["B"] => string (5) "brown" ["c"] => string (4) "blue" [0] => string (3) "red "}

10. fill the array with the given value -- array_fill

Usage: array_fill ($ start_index (int), $ num (int), $ value) uses the value of the value parameter to fill an array with num entries. The key name starts with the specified start_index parameter. Note that num must be a value greater than zero.

var_dump(array_fill(5, 6, 'banana'));

Result: array (4) {[5] => string (6) "banana" [6] => string (6) "banana" [7] => string (6) "banana" [8] => string (6) "banana "}

11. fill the array with the specified key and value -- array_fill_keys

Array_fill_keys ($ input (array), $ value (string) uses the value of the value parameter as the value, and uses the value of the keys array as the key to fill an array.

$keys = array('foo', 5, 10, 'bar'=>'1');
var_dump(array_fill_keys($keys, 'banana'));

Result: array (4) {["foo"] => string (6) "banana" [5] => string (6) "banana" [10] => string (6) "banana" [1] => string (6) "banana "}

12. use the callback function to filter the unit array_filter in the array.

Array_filter ($ input (array), $ callback (function). The value in $ input is passed to the callback function. the callback function is used for judgment and then returned to the result array.

function odd($var){
return($var % 2 == 1);
}
function even($var){
return($var % 2 == 0);
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
var_dump(array_filter($array1, "odd"));
var_dump(array_filter($array2, "even"));

Result: array (3) {["a"] => int (1) ["c"] => int (3) ["e"] => int (5 )}

Array (4) {[0] => int (6) [2] => int (8) [4] => int (10) [6] => int (12 )}

13. switch the key and value in the array-array_flip ($ input (array). It seems that this is mainly used to associate the array.

Usage: enter an array where the exchange key is very key-value. The key name must be valid, for example, string or int type,

If the value type is incorrect, a warning will be issued, and the problematic key/value pair will not be reversed. If the same value appears multiple times, the last key name is used as its value, and all others are lost. Array_flip () returns FALSE if it fails.

$array = array("javascript","f"=>"redis","g"=>"R","h"=>"mongodb");
var_dump(array_flip($array));

Result: array (4) {["javascript"] => int (0) ["redis"] => string (1) "f" ["R"] => string (1) "g" ["mongodb"] => string (1) "h "}

14. calculate the intersection of arrays-array_intersect

Usage: array_intersect (array1, array2. ..) is used to calculate the intersection of multiple arrays and return the result as an array.

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
var_dump(array_intersect($array1, $array2));

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

15. Comparison with callback functionsKey nameTo calculate the intersection of arrays-array_intersect_ukey

Array_intersect_ukey (array1, array2,..., function) calculates the intersection of arrays through the callback function.

This comparison is performed through the callback function provided by the user. If the first parameter is considered to be less than, equal to, or greater than the second parameter, an integer less than zero, equal to zero, or greater than zero must be returned.

function key_compare_func($key1, $key2){
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));

Result: array (2) {["blue"] => int (1) ["green"] => int (3 )}

16. check and calculate the intersection of arrays with indexes. use the callback function to compare indexes-array_intersect_uassoc

The array returned by array_intersect_uassoc (array1, array2,..., function) contains all values that appear in the array of all other parameters in array1 at the same time.

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));

Result: Array ([B] => brown)

17. use the key name to compare and calculate the intersection of arrays -- array_intersect_key

Usage: The array returned by array_intersect_key (array1, array2,...) contains the values of all key names that appear in array1 and all other parameter arrays at the same time.

$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_key($array1, $array2));

Result: array (2) {["blue"] => int (1) ["green"] => int (3 )}

18. array_intersect_assoc

Usage: like array_diff_assoc, the difference is that an intersection and a Union

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$array3 = array("a" => "green", "red", "b" => "brown", "c" => "blue");
$array4 = array("a" => "green", "red", "yellow");
var_dump(array_intersect_assoc($array1, $array2));
var_dump(array_intersect_assoc($array3, $array4));

Result:

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

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.