Detailed explanation of some functions of php

小云云
Release: 2023-03-22 14:56:01
Original
1136 people have browsed it

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);
?>
Copy after login

Print results:

array (size=2)
  4 => string &#39;this_4&#39; (length=6)
  5 => string &#39;this_5&#39; (length=6)
Copy after login

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);
?>
Copy after login

Print Result:

array (size=4)
  0 => int 2
  1 => int 3
  2 => int 4
  3 => int 5
Copy after login

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!";
}
?>
Copy after login

Output result:

yes!
Copy after login

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;
?>
Copy after login

Output result:

4
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!