Home >Backend Development >PHP Problem >In-depth analysis of array_keys() in PHP

In-depth analysis of array_keys() in PHP

autoload
autoloadOriginal
2021-04-16 09:49:512506browse

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   )

  • $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));
输出:Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 )

b. There are only two parameters

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

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));
?>
输出:Array ( [0] => 4 [1] => 5 )
      Array ( [0] => 4 )

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!

Statement:
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