array keys php,php array_keys与array_search的简单使用
函數(shù)說明
array_keys():返回?cái)?shù)組中所有的鍵名。
array_search():在數(shù)組中搜索給定的值,如果成功則返回相應(yīng)的鍵名。
//根據(jù)一個(gè)key返回關(guān)聯(lián)數(shù)組中的另一個(gè)key,并且不使用foreach
// function array_key_relative(array $array, string $current_key, int $offset)
function array_key_relative($array, $current_key, $offset = 1) {
// create key map
$keys = array_keys($array);
// find current key
$current_key_index = array_search($current_key, $keys);
// return desired offset, if in array, or false if not
if(isset($keys[$current_key_index + $offset])) {
return $keys[$current_key_index + $offset];
}
return false;
}
//Usage example:
$test_array = array(
"apple" => "Red, shiny fruit",
"orange" => "Orange, dull, juicy fruit",
"pear" => "Usually green and odd-shaped fruit",
"banana" => "Long yellow fruit that monkeys like to eat",
"cantelope" => "Larger than a grapefruit",
"grapefruit" => "Kind of sour"
);
echo array_key_relative($test_array, "apple", 2); // outputs "pear"
echo array_key_relative($test_array, "orange", -1); // outputs "apple" */
$next_key = array_key_relative($test_array, "banana", 1); // Get the key after banana (cantelope)
echo $test_array[$next_key]; // outputs "Larger than a grapefruit"
?>
總結(jié)
以上是生活随笔為你收集整理的array keys php,php array_keys与array_search的简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Linux 系统备份恢复工具 SYSTE
 - 下一篇: SVM多分类原理学习