Home  >  Article  >  Backend Development  >  PHP array operation to obtain array element index value_PHP tutorial

PHP array operation to obtain array element index value_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:492668browse

If we want to quickly obtain the index value by giving a value, we can use the php array_values() function, which can help us find what we want quickly and concisely. Let's take a look at the usage of the array_values() function

The array_keys() function returns a new array containing all the key names in the array.

If the second parameter is provided, only the key name with the key value is returned.

If the strict parameter is specified as true, PHP will use equality comparison (===) to strictly check the data type of the key value.

Grammar
array_keys(array,value) parameter description
array required. Specifies the input array.
value is optional. The index (key) of the specified value.
strict optional. Used with the value parameter. Possible values:

true - Returns the key with the specified value based on the type.
false - the default value. Does not depend on type.

Example 1

The code is as follows
 代码如下 复制代码

$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a));
?>

输出:

Array ( [0] => a [1] => b [2] => c )

Copy code


 代码如下 复制代码

$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a,"Dog"));
?>

输出:

Array ( [0] => c)

$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");

print_r(array_keys($a));
?>

 代码如下 复制代码

$a=array(10,20,30,"10");
print_r(array_keys($a,"10",false));
?>

输出:

Array ( [0] => 0 [1] => 3 )

Output:


Array ( [0] => a [1] => b [2] => c )

 代码如下 复制代码

$a=array(10,20,30,"10");
print_r(array_keys($a,"10",true));
?>

输出:

Array ( [0] => 3)

Example 2 Use value parameter:

The code is as follows
Copy code
$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog"); print_r(array_keys($a,"Dog")); ?> Output: Array ( [0] => c) Example 3
Use strict parameter (false):
The code is as follows Copy code
$a=array(10,20,30,"10");<🎜> print_r(array_keys($a,"10",false));<🎜> ?> Output: Array ( [0] => 0 [1] => 3 ) Example 4 Use strict parameter (true):
The code is as follows Copy code
$a=array(10,20,30,"10");<🎜> print_r(array_keys($a,"10",true));<🎜> ?> Output: Array ( [0] => 3) http://www.bkjia.com/PHPjc/631307.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631307.htmlTechArticleIf we want to quickly obtain the index value by giving a value, we can use the php array_values() function, which It can help us find what we want quickly and concisely. Let’s find out...
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