array_keys()是PHP中的內置函數,用於返回key和array的所有鍵或鍵的子集。
用法:
array array_keys($input_array, $search_value, $strict)
參數:該函數帶有三個參數,其中一個是必需參數,另外兩個是可選參數。
- $input_array(強製性):指我們要操作的陣列。
- $search_value(可選):指向我們要在數組中搜索關鍵元素的數組的值。如果傳遞了此參數,則該函數將僅返回與此元素對應的鍵核心,否則它將返回數組的所有鍵。
- $strict(可選):確定在搜索過程中是否應使用嚴格比較(===)。默認值為false。
返回值:該函數返回一個包含所有鍵或鍵子集的數組,該數組取決於傳遞的參數。
例子:
Input : $input_array = ("one" => "shyam", 2 => "rishav",
"three" => "gaurav")
Output :
Array
(
[0] => one
[1] => 2
[2] => three
)
Input : $input_array = ("one", "two", "three", "one",
"four", "three", "one", "one")
$search_value = "one"
Output :
Array
(
[0] => 0
[1] => 3
[2] => 6
[3] => 7
)
在下麵的程序中,我們向函數array_keys()傳遞了一個簡單的關聯數組,以打印其所有鍵:
<?php
// PHP function to illustrate the use of array_keys()
function get_Key($array)
{
$result = array_keys($array);
return($result);
}
$array = array("one" => "shyam", 2 => "rishav",
"three" => "gaurav");
print_r(get_Key($array));
?>輸出:
Array
(
[0] => one
[1] => 2
[2] => three
)
在下麵的程序中,我們連同數組一起傳遞了一個僅返回鍵位置的值。
<?php
// PHP function to illustrate the use of array_keys()
function get_Key($array, $search_value)
{
$result = array_keys($array, $search_value);
return($result);
}
$array = array("one", "two", "three", "one", "four",
"three", "one", "one");
$search_value = "one";
print_r(get_Key($array, $search_value));
?>輸出:
Array
(
[0] => 0
[1] => 3
[2] => 6
[3] => 7
)
參考: http://php.net/manual/en/function.array-keys.php
相關用法
- p5.js nfc()用法及代碼示例
- p5.js nfp()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
- p5.js nfs()用法及代碼示例
- PHP cos( )用法及代碼示例
- PHP sin( )用法及代碼示例
- p5.js nf()用法及代碼示例
- PHP tan( )用法及代碼示例
- PHP pow( )用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- d3.js d3.set.has()用法及代碼示例
- PHP Ds\Set xor()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 PHP | array_keys() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
