A detailed explanation for the use of array arrays in PHP (1/2)

Source: Internet
Author: User
Tags arrays comparison

Introduction: This is the introduction of the PHP manual for the various operations of the system function, you can say that the array in PHP has an important role, so the function is also more gray, the following Tianya the most commonly used for detailed description.

array_change_key_case-returns an array with a string key that is all 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 Copy Code
<?php
$phpha = Array (' Home ' => ' http://www.111cn.net ', ' Blog ' => ' http://www.111cn.net ');
$phpha _upper = Array_change_key_case ($phpha, Case_upper);
$phpha _lower = Array_change_key_case ($phpha, case_lower); Default condition
Print_r ($phpha _upper);
Print_r ($phpha _lower);
?>
Results:
Array
(
[Home] => http://www.111cn.net
[BLOG] => http://www.111cn.net
)
Array
(
[Home] => http://www.111cn.net
[Blog] => http://www.111cn.net
)

array_chunk-to split an array into multiple

Array Array_chunk (array $input, int $size [, bool $preserve _keys])

Array_chunk () splits an array into multiple arrays, where the number of units per array is determined by size. The number of cells in the last array may be few. The resulting array is a cell in a multidimensional array whose index starts at zero.
Setting the optional parameter Preserve_keys to TRUE allows PHP to retain the original key name in the input array. If you specify FALSE, then each result array will be indexed with a new zero-based number. The default value is FALSE.

The code is as follows Copy Code
<?php
$input _array = Array (' A ', ' B ', ' C ', ' d ', ' e ');
Print_r (Array_chunk ($input _array, 2));
Print_r (Array_chunk ($input _array, 2, TRUE));
?>
Results:
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => C
[1] => D
)

[2] => Array
(
[0] => E
)

)
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[2] => C
[3] => D
)

[2] => Array
(
[4] => E
)

)

Array_combine-creates an array with the value of one array as its key name and the value of the other array as its value

Array Array_combine (array $keys, array $values)

Returns an array that uses the value from the keys array as the key name, and the value from the values array as the corresponding value.
Returns FALSE if the number of cells in the two arrays is different or the array is empty.

The code is as follows Copy Code
     <?php
    $key = Array (' Home ', ' blog ');
    $key 2 = array (' Home ', ' blog '), ' BBS ');
    $phpha = Array (' http://www.111cn.net ', ' http://www.111cn.net ');
    $phpha _combine = Array_combine ($key, $phpha);
    $phpha _combine_wrong = Array_combine ($key 2, $phpha);
    Print_r ($phpha _combine);
    Print_r ($phpha _combine_wrong);
   
   /results:
    Array
    (
& nbsp;   [Home] => http://www.111cn.net
    [Blog] => http://www.111cn.net
   )
   

You can see the second array_combine () error, which indicates that the number of members in the 2 arrays is unequal
Warning:both parameters should have an equal number of elements to F:program on line 31

array_count_values-counts 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 value that appears in the input array as a value.

  code is as follows copy code
  "." PHP
    $phpha = array (' Hello ', ' world ', ' Tianya ', ' Hello ', ' world ');
    $phpha _ result = Array_count_values ($phpha);
    Print_r ($phpha _result);
   
   /results:
    Array
    (
& nbsp;   [Hello] => 2
    [World] => 2
    [Tianya] => 1
  ;  )

array_diff-Calculating the difference set of an array
array_diff_key-the difference set of an array using a key name comparison
array_diff_ukey-Comparison of a key name with a callback function to compute the difference set of an array
array_diff_assoc-the difference set of an array computed with an index check
array_diff_uassoc-an index check using user-supplied callback functions to compute the difference set of an array

The code is as follows Copy Code
Array_diff () returns an array that includes all of the Array1
But not the value in any other parameter array. Note that the key name remains unchanged.
<?php
$array 1 = Array ("A" => "green", "Red", "Blue", "Red");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_diff ($array 1, $array 2);
Print_r ($result);
?>
Results:
Array
(
[1] => Blue
)
This function is the same as Array_diff () except that the comparison is based on the key name instead of the value.
<?php
$array 1 = Array (' Blue ' => 1, ' Red ' => 2, ' Green ' => 3, ' Purple ' => 4);
$array 2 = Array (' Green ' => 5, ' Blue ' => 6, ' Yellow ' => 7, ' cyan ' => 8);
Print_r (Array_diff_key ($array 1, $array 2));
?>
Results:
Array
(
[Red] => 2
[Purple] => 4
)
Note that unlike Array_diff (), the key name is also used for comparison.
<?php
$array 1 = Array ("A" => "green", "B" => "Brown", "C" => "Blue", "Red");
$array 2 = Array ("A" => "green", "yellow", "red");
Print_r (Array_diff_assoc ($array 1, $array 2));
?>
Results:
Array
(
[B] => Brown
[C] => Blue
[0] => Red
)

Array_fill-fills the array with the given value
Array_fill_keys-fill an array with values, specifying keys

Array_filter-using callback functions to filter cells in an array

The code is as follows Copy Code

<?php
function Func_check ($i) {
return $i > 3? True:false;
}
$array 1 = Array (2, 3, 5, 6);
$array 2 = Array (NULL, ', ' hello ');
$array 3 = Array_filter ($array 1, ' Func_check ');
$array 4 = Array_filter ($array 2);
function Func_check () is used to determine a given value, return TRUE or False
Returns True, the value in $array1 is returned and the key name is unchanged, otherwise filtered out
Print_r ($array 3);
If you do not specify a callback function, the default is to filter out members in Array2 that are equivalent to False
for type conversion. Therefore, this function often concerns the empty members of the array.
Print_r ($array 4);
?>
Results:
Array
(
[2] => 5
[3] => 6
)
Array
(
[2] => Hello
)

array_flip-the keys and values in the swap array

  code is as follows copy code
  / /If the same value appears more than once, the last key will be its value and all others will be lost.
    <?php
    $trans = Array ("A" => 1, "B" => 1, "C" => 2);
 & nbsp;  $trans = Array_flip ($trans);
    Print_r ($trans);
   
   /results:
    Array
    (
& nbsp;   [1] => b
    [2] => C
   )

array_intersect-compute the intersection of arrays
array_intersect_assoc-the intersection of computed arrays with index checking
array_intersect_uassoc-the intersection of computed arrays with index checking, using callback functions to compare indexes
Array_intersect_key-computes the intersection of an array using a key name comparison
Array_intersect_ukey-computes the intersection of an array using a callback function to compare the key name

The code is as follows Copy Code
<?php
$array 1 = Array ("A" => "green", "Red", "blue");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
Print_r ($result);
?>
Results:
Array
(
[A] => green
[0] => Red
)
Note that the ARRAY_INTERSECT_ASSOC () and Array_intersect () differ in that the key name is also used for comparison.
<?php
$array 1 = Array ("A" => "green", "B" => "Brown", "C" => "Blue", "Red");
$array 2 = Array ("A" => "green", "yellow", "red");
$result = Array_intersect_assoc ($array 1, $array 2);
Print_r ($result);
?>
Results:
Array
(
[A] => green
)

Home 1 2 last page

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.