php数组搜索(多维数组搜索)
在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,如果找到,将输出这个水果的颜色,代码如下:
<?php $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),如果找到,则返回有关信息,代码如下:
<?php $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 [, mixedsearch_value]) ?>
如果包含可选参数search_value,则只会返回与该值匹配的键,下面的例子将输出$fruit数组中找到的所有数组,代码如下:
<?php $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 )
上面讲到的都只是一维数组搜索了,如果要实现二维数据或多维数据我们可参考下面实例.
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 怎么进行查找呢,有三个结果,而这三个结果都要,看下面的函数:
<?php 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; } ?>
这个函数就可以把上面要查找到的内容全部返回出键名来.
<?php $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搜索多维数组的键名,代码如下:
<?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 ) )
本文地址:http://www.phprm.com/shuzu/fs1407.html
转载随意,但请附上文章地址:-)