Explanation of Array function usage in php (12 ). In php, arrays are a special data type. php also provides us with a large number of array operation functions, which makes us feel no pressure when operating arrays, below I have summarized that Arrays in php are a special data type, and php also provides us with a large number of array operation functions, as a result, we feel no pressure on array Operations. below I will summarize the php array getting started notes and share them with you.
Introduction: This article describes the system functions used in the PHP Manual to perform various operations on arrays. it can be said that arrays play an important role in PHP, so the functions are also very gray, the following describes the most commonly used methods.
Array_change_key_case-returns an array of string keys in lowercase or uppercase.
Array array_change_key_case (array $ input [, int $ case])
$ Case can be CASE_UPPER or CASE_LOWER (default)
| The code is as follows: |
|
$ Phpha = array ('Home' => 'http: // www. bKjia. c0m', 'blog '=> 'http: // www. bKjia. c0m ');
$ Pha_upper = array_change_key_case ($ phpha, CASE_UPPER );
$ Phpha_lower = array_change_key_case ($ phpha, CASE_LOWER); // Default
Print_r ($ pha_upper );
Print_r ($ pha_lower );
?>
// Result:
Array
(
[HOME] => http://www.bKjia. c0m
Http://www.bKjia. c0m [BLOG] =>
)
Array
(
[Home] => http://www.bKjia. c0m
Http://www.bKjia. c0m [blog] =>
) |
Array_chunk-divide an array into multiple
Array array_chunk (array $ input, int $ size [, bool $ preserve_keys])
Array_chunk () splits an array into multiple arrays. the number of units in each array is determined by the size. The number of units in the last array may be smaller. The obtained array is a unit in a multi-dimensional array, and its index starts from scratch.
Set the optional parameter preserve_keys to TRUE so that PHP retains the original key name in the input array. If you specify FALSE, each result array is indexed with a new number starting from scratch. The default value is FALSE.
| The code is as follows: |
|
$ Input_array = array ('A', 'B', 'C', 'D', 'E ');
Print_r (array_chunk ($ input_array, 2 ));
Print_r (array_chunk ($ input_array, 2, TRUE ));
?>
// Result:
Array
(
[0] => Array
(
[0] =>
[1] => B
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Array
(
[0] => Array
(
[0] =>
[1] => B
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
) |
Array_combine-creates an array and uses the value of one array as its key name, and the value of another array as its value
Array array_combine (array $ keys, array $ values)
Returns an array with the value from the keys array as the key name and the value from the values array as the corresponding value.
If the number of cells in two arrays is different or the array is empty, FALSE is returned.
| The code is as follows: |
|
$ Key = array ('Home', 'blog ');
$ Key2 = array ('Home', 'blog ', 'bbs ');
$ Phpha = array ('http: // www. bKjia. c0m', 'http: // www. bKjia. c0m ');
$ Phpha_combine = array_combine ($ key, $ phpha );
$ Phpha_combine_wrong = array_combine ($ key2, $ phpha );
Print_r ($ pha_combine );
Print_r ($ pha_combine_wrong );
?>
// Result:
Array
(
[Home] => http://www.bKjia. c0m
Http://www.bKjia. c0m [Blog] =>
)
|
// The second array_combine () error is reported, indicating that the number of members of the two arrays is not equal.
Warning: Both parameters shocould have an equal number of elements in F: Program FilesPHPNOWhtdocsindex. php on line 31
Array_count_values-count the number of occurrences of all values in the array
Array array_count_values (array $ input)
Array_count_values () returns an array that uses the value in the input array as the key name and the number of times this value appears in the input array as the value.
| The code is as follows: |
|
$ Phpha = array ('hello', 'World', 'tianya ', 'hello', 'World ');
$ Phpha_result = array_count_values ($ phpha );
Print_r ($ pha_result );
?>
// Result:
Array
(
[Hello] => 2
[World] => 2
[Tianya] => 1
) |
Array_diff-calculate the difference set of the array
Array_diff_key-use the key name to compare the difference set of the calculated array
Array_diff_ukey-use the callback function to compare the difference set of the calculated array with the key name
Array_diff_assoc-calculate the array's difference set with index check
Array_diff_uassoc-use the callback function provided by the user to perform an index check to calculate the array's difference set
| The code is as follows: |
|
// Array_diff () returns an array containing all
// But not in any other parameter array. Note that the key name remains unchanged. $ Array1 = array ("a" => "green", "red", "blue", "red ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_diff ($ array1, $ array2 );
Print_r ($ result );
?>
// Result:
Array
(
[1] => blue
)
// This function is the same as array_diff () except that the comparison is based on the key name rather than the value. $ Array1 = array ('blue' => 1, 'Red' => 2, 'green' => 3, 'Purple '=> 4 );
$ Array2 = array ('green' => 5, 'blue' => 6, 'yellow' => 7, 'cya' => 8 );
Print_r (array_diff_key ($ array1, $ array2 ));
?>
// Result:
Array
(
[Red] => 2
[Purple] => 4
)
// Note that, unlike array_diff (), the key name is also used for comparison. $ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red ");
$ Array2 = array ("a" => "green", "yellow", "red ");
Print_r (array_diff_assoc ($ array1, $ array2 ));
?>
// Result:
Array
(
[B] => brown
[C] => blue
[0] => red
) |
Array_fill-fill the array with the given value
Array_fill_keys-Fill an array with values, specifying keys
Array_filter-filter elements in the array using the callback function
| The code is as follows: |
|
|
Function func_check ($ I ){
Return $ I> 3? TRUE: FALSE;
}
$ Array1 = array (2, 3, 5, 6 );
$ Array2 = array (NULL, '', 'Hello ');
$ Array3 = array_filter ($ array1, 'func_check ');
$ Array4 = array_filter ($ array2 );
// Function func_check () is used to determine the given value. TRUE or FALSE is returned.
// If TRUE is returned, the value in $ array1 is returned and the key name remains the same; otherwise, it is filtered out.
Print_r ($ array3 );
// If no callback function is specified, the members in array2 are filtered out by default.
// Type conversion. Therefore, this function is usually used to concern about empty members in the array.
Print_r ($ array4 );
?>
// Result:
Array
(
[2] => 5
[3] => 6
)
Array
(
[2] => hello
) |
Array_flip-swap the keys and values in the array
| The code is as follows: |
|
// If the same value appears multiple times, the last key name is used as its value, and all others are lost. $ Trans = array ("a" => 1, "B" => 1, "c" => 2 );
$ Trans = array_flip ($ trans );
Print_r ($ trans );
?>
// Result:
Array
(
[1] => B
[2] => c
) |
Array_intersect-calculates the intersection of arrays
Array_intersect_assoc-intersection of arrays computed by checking indexes
Array_intersect_uassoc-returns the intersection of arrays computed by checking indexes and compares indexes with callback functions.
Array_intersect_key-use a key name to compare and calculate the intersection of arrays
Array_intersect_ukey-use the callback function to compare key names to calculate the intersection of arrays
| The code is as follows: |
|
$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_intersect ($ array1, $ array2 );
Print_r ($ result );
?>
// Result:
Array
(
[A] => green
[0] => red
)
// Note that what is different between array_intersect_assoc () and array_intersect () is that the key name is also used for comparison. $ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red ");
$ Array2 = array ("a" => "green", "yellow", "red ");
$ Result = array_intersect_assoc ($ array1, $ array2 );
Print_r ($ result );
?>
// Result:
Array
(
[A] => green
) |
1 2
...