This article mainly shares with you the detailed explanation of some functions of PHP, mainly in the form of code. I hope it can help you.
1. array_diff_assoc($arr1,$arr2,$arr3...) function: compare the key names and key values of two arrays and return the difference set
Example:
<?php $a1=array("2"=>"this_2","3"=>"this_3","4"=>"this_4","5"=>"this_5"); $a2=array("1"=>"this_1","2"=>"this_2","3"=>"this_3"); $result1 = array_diff_assoc($a1,$a2); //数组可以交换顺序滴,也可以是多个数组 var_dump($result1); ?>
Print results:
array (size=2) 4 => string 'this_4' (length=6) 5 => string 'this_5' (length=6)
2. array_keys() function: Return a new array containing all the key names in the array
Example:
<?php $a1=array("2"=>"this_2","3"=>"this_3","4"=>"this_4","5"=>"this_5"); $result2 = array_keys($a1); var_dump($result2); ?>
Print Result:
array (size=4) 0 => int 2 1 => int 3 2 => int 4 3 => int 5
3. array_key_exists() function: Check whether the specified key name exists in an array. If the key name exists, it returns true. If the key name does not exist, it returns false.
Example:
<?php $a1=array("2"=>"this_2","3"=>"this_3","4"=>"this_4","5"=>"this_5"); if (key_exists("2",$a1)){ echo "yes!"; }else{ echo "no!"; } ?>
Output result:
yes!
4. sort() function: Sort the array in ascending order
5. rsort() function: Sort the array in descending order
6. asort() function: Sort the associative array in ascending order according to the value
7. ksort() function: Sort the associative array in ascending order according to the key Sorting
8. arsort() function: Sort the associative array in descending order according to the value
9. krsort() function: Sort the associative array in descending order according to the key
10. count() function: returns the number of elements in the array
Example:
<?php $a1=array("2"=>"this_2","3"=>"this_3","4"=>"this_4","5"=>"this_5"); $result3 = count($a1); echo $result3; ?>
Output result:
4
Related recommendations:
Advanced explanation of php functions
Introduction to PHP function examples
Search performance test of php functions
The above is the detailed content of Detailed explanation of some functions of php. For more information, please follow other related articles on the PHP Chinese website!