In-depth analysis of array_keys() in PHP

autoload
Release: 2023-03-09 11:26:02
Original
2476 people have browsed it

In-depth analysis of array_keys() in PHP

Arrays are a common data type in PHP. During normal use, we may often need to obtain the key name of the array. PHP provides array_keys() This article will take you to take a look at this function.

First, let’s take a look at the syntax of <span style="font-family: 微软雅黑, " microsoft yahei background-color: rgb color:>array_keys()</span><span style="font-family: 微软雅黑, " microsoft yahei background-color: rgb color:></span><span style="font-family: 微软雅黑, " microsoft yahei background-color: rgb color:></span>##.

array_keys ( array $array   , mixed $search_value = null   , bool $strict = false   )
Copy after login

  • $array: The array to be viewed.

  • $search_vaule: The default value is empty. If this parameter is specified, only key names containing these values ​​will be returned.

  • $strict: Determine whether strict comparison (===) should be used when searching

  • Return value: index array of array type

Actual experience:


a. There is only one parameter:

<?php
     $ace=array("one","two","three","four","Three");
     print_r(array_keys($ace));
Copy after login
输出:Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 )
Copy after login

b. There are only two parameters

<?php
   $ace=array("one","two","three","four","Three");
    print_r(array_keys($ace,"three"));
?>
Copy after login
输出:Array ( [0] => 2 )
Copy after login

c. There are three parameters:

<?php
   $ace2=array("one","two","three","four","10",10);
     print_r(array_keys($ace2,"10"));
      echo "<br>";
     print_r(array_keys($ace2,"10",true));
?>
Copy after login
输出:Array ( [0] => 4 [1] => 5 )
      Array ( [0] => 4 )
Copy after login

We can see that when the third parameter is

true, array_keys () Enables a more stringent comparison.

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of In-depth analysis of array_keys() in 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