PHP Array function usage detailed (_php) tutorial

Source: Internet
Author: User
In PHP, the array is a special type of data, and PHP also provides us with a large number of array manipulation functions, so that we do not feel pressure in the operation of arrays, the following I summed up the PHP array of entry notes to share to you.

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

array_change_key_case-returns an array of string key names that are all lowercase or uppercase

Array array_change_key_case (array $input [, int $case])

$case can be Case_upper or case_lower (default)

copy code
!--? php
$phpha = Array (' Home ' = ' http ://www.bkjia.c0m ', ' Blog ' = ' http://www.bKjia.c0m ');
$phpha _upper = Array_change_key_case ($phpha, Case_upper);
$phpha _lower = Array_change_key_case ($phpha, case_lower);//default
Print_r ($phpha _upper);
Print_r ($phpha _ Lower);
?
//Result:
Array
(
[HOME] = http://www.bKjia.c0m
[BLOG] + http://www.bKjia.c0m
)
Array
(
[Home] = http://www.bKjia.c0m
[Blog] = http://www.bKjia.c0m
)

array_chunk-splits 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 cells 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 keep 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.

code 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));
?
//Result:
Array
(
[0] = + Array
(
[0] = a
[1] = = b
)

[1] + = Ar Ray
(
[0] = = C
[1] + D
)

[2] = = = Array
(
[0] + e
)

)
array< br> (
[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 an array as its key name, and the value of another array as its value

Array Array_combine (array $keys, array $values)

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

copy code
$key = Array (' Home ', ' Blog '); br> $key 2 = 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 ($key 2, $phpha);
Print_r ($phpha _combine);
Print_r ($phpha _combine_wrong);
?
//Result:
Array
(
[Home] = http://www.bKjia.c0m
[Blog] + http://www.bKjia.c0m
)

You can see the second array_combine () error, indicating that the number of members of 2 arrays is unequal
Warning:both parameters should a equal number of elements in F:program filesphpnowhtdocsindex.php on line 31

array_count_values-the number of occurrences of all values in the statistics 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 occurrences in the input array as the value.

The code is as follows Copy Code
$phpha = Array (' Hello ', ' world ', ' Tianya ', ' Hello ', ' world ');
$phpha _result = array_count_values ($phpha);
Print_r ($phpha _result);
?>
Results:
Array
(
[Hello] = 2
[World] = 2
[Tianya] = 1
)

array_diff-Calculating the difference set of an array
array_diff_key-Calculating the difference set of an array using the key name comparison
array_diff_ukey-using a callback function to compare the difference set of an array to a key name
array_diff_assoc-with index Check the difference set of the computed array
array_diff_uassoc-uses the user-supplied callback function to do an index check to calculate the difference set of the 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 The key name remains unchanged.
$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 and not the value.
$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.
$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 an array with the given value
Array_fill_keys-fill an array with values, specifying keys

array_filter-filter cells in an array with callback functions

The code is as follows Copy Code

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);
The function Func_check () is used to determine the given value, which returns TRUE or False
Returns True, the value in $array1 is returned and the key name is not changed, otherwise it is filtered out
Print_r ($array 3);
If you do not specify a callback function, the default is to filter out members that are equivalent to false in Array2
for type conversion. So the usual amount of this function concerns the empty members in the array.
Print_r ($array 4);
?>
Results:
Array
(
[2] = 5
[3] = 6
)
Array
(
[2] = = Hello
)

Array_flip-exchanging keys and values in an array

The code is as follows Copy Code
If the same value occurs more than once, the last key will be its value, and all the others are lost.
$trans = Array ("a" = = 1, "b" = 1, "c" = 2);
$trans = Array_flip ($trans);
Print_r ($trans);
?>
Results:
Array
(
[1] = b
[2] = C
)

Array_intersect-computes the intersection of an array
array_intersect_assoc-with index Check the intersection of computed arrays
array_intersect_uassoc-with index Check the intersection of computed arrays, using callback function to compare indexes
array_intersect_key-calculating the intersection of an array using the key name comparison
Array_intersect_ukey-computes the intersection of an array using a callback function to compare the key names

code 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);
?
//Result:
Array
(
[A] = green
[0] = Red
)
//Note ARRAY_INTERSECT_ASSOC () and Array_int Ersect () Different is 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);
?
//Result:
Array
(
[A] = green
)

1 2

http://www.bkjia.com/PHPjc/632651.html www.bkjia.com true http://www.bkjia.com/PHPjc/632651.html techarticle in PHP, the array is a special type of data, and PHP also provides us with a large number of array manipulation functions, so that we do not feel the pressure in the array operation Ah, the following I summed up ...

  • Related Article

    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.