在php中要實現數組搜索方法有很多種,我知道最簡單的就是in_array與遍歷數組後再一個個對比,其它還有更多更好方法,下面我來介紹介紹。
一維數組搜索很簡單in_array()
如果 value 參數是字符串,且 type 參數設置為 true,則搜索區分大小寫
代碼如下 復制代碼<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn",$people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
輸出:
Match found
array_key_exists()函數
如果在一個數組中找到一個指定的鍵,函數array_key_exists()返回true,否則返回false。其形式如下:
boolean array_key_exists(mixed key,array array);
下面的例子將在數組鍵中搜索apple,如果找到,將輸出這個水果的顏色:
代碼如下 復制代碼
$fruit["apple"] = "red";
$fruit["banana"] = "yellow";
$fruit["pear"] = "green";
if(array_key_exists("apple", $fruit)){
printf("apple's color is %s",$fruit["apple"]);
}
//apple's color is red
array_search()函數
array_search()函數在一個數組中搜索一個指定的值,如果找到則返回相應的鍵,否則返回false。其形式如下:
mixed array_search(mixed needle,array haystack[,boolean strict])
下面的例子在$fruits中搜索一個特定的日期(December 7),如果找到,則返回相應州的有關信息:
代碼如下 復制代碼
$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$founded = array_search("green", $fruits);
if($founded)
printf("%s was founded on %s.",$founded, $fruits[$founded]);
//watermelon was founded on green.
array_keys()函數
array_keys()函數返回一個數組,其中包含所搜索數組中找到的所有鍵。其形式如下:
array array_keys(array array[,mixed search_value])
如果包含可選參數search_value,則只會返回與該值匹配的鍵。下面的例子將輸出$fruit數組中找到的所有數組:
$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$keys = array_keys($fruits);
print_r($keys);
//Array ( [0] => apple [1] => banana [2] => watermelon )
array_values()函數
array_values()函數返回一個數組中的所有值,並自動為返回的數組提供數值索引。其形式如下:
array array_values(array array)
下面的例子將獲取$fruits中找到的各元素的值:
代碼如下 復制代碼
$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$values = array_values($fruits);
print_r($values);
//Array ( [0] => red [1] => yellow [2] => green )
上面講到的都只是一維數組搜索了,如果要實現二維數據或多維數據我們可參考下面實例
1 php搜索多維數組的鍵值
如下面例子:
代碼如下 復制代碼$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo[3]['c']['dd'] = 'bar 3';
$foo[3]['f']['gg'] = 'bar 3';
$foo['info'][1] = 'bar 5';
如果要查找 bar 3 怎麼進行查找呢。有三個結果,而這三個結果都要,看下面的函數:
-------------------------------------------------------------------------------------------------------------------------------
function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){
global $nodes_found;
$a++;
foreach ($haystack as $key1=>$value1) {
$nodes_temp[$a] = $key1;
if (is_array($value1)){
array_search_re($needle, $value1, $a, $nodes_temp);
}
else if ($value1 === $needle){
$nodes_found[] = $nodes_temp;
}
}
return $nodes_found;
}
---------------------------------------------------------------------------------------------------------------------------------
這個函數就可以把上面要查找到的內容全部返回出鍵名來
$result = array_search_re('bar 3', $foo);
print_r($result);
輸出結果為如下:
Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb )
[1] => Array ( [1] => 3 [2] => c [3] => dd )
[2] => Array ( [1] => 3 [2] => f [3] => gg )
)
php搜索多維數組的鍵名
代碼如下 復制代碼function array_search_key($needle, $haystack){
global $nodes_found;
foreach ($haystack as $key1=>$value1) {
if ($key1=== $needle){
$nodes_found[] = $value1;
}
if (is_array($value1)){
array_search_key($needle, $value1);
}
}
return $nodes_found;
}
$result = array_search_key('a', $foo);
print_r($result);
輸出結果為如下:
Array
(
[0] => Array
(
[xx] => bar 1
)
[1] => Array
(
[bb] => bar 3
)
[2] => Array
(
[yy] => bar 4
)
)