频道栏目
首页 > 资讯 > 数组 > 正文

php中数组的搜索程序代码

16-01-06        来源:[db:作者]  
收藏   我要投稿

在php中简单数据搜索很简单我们直接使用in_array() 函数在数组中搜索给定的值即可,这种是简单的一维数据,例如下代码:

  1. $people = array("Peter", "Joe", "Glenn", "Cleveland"); 
  2. if (in_array("Glenn",$people)) 
  3.   { 
  4.   echo "Match found"; 
  5.   } 
  6. else 
  7.   { 
  8.   echo "Match not found"; 
  9.   } 
  10. ?> 

输出:Match found。

array_key_exists()函数

如果在一个数组中找到一个指定的键,函数array_key_exists()返回true,否则返回false,其形式如下:

boolean array_key_exists(mixed key,array array);

下面的例子将在数组键中搜索apple,如果找到,将输出这个水果的颜色,代码如下:

  1. $fruit["apple"] = "red";  
  2.  $fruit["banana"] = "yellow";  
  3.  $fruit["pear"] = "green";  
  4.  if(array_key_exists("apple", $fruit)){  
  5.   printf("apple's color is %s",$fruit["apple"]);  
  6.  } 

执行这段代码得到的结果:apple's color is red

array_search()函数,代码如下:

  1. $fruits["apple"] = "red"; 
  2. $fruits["banana"] = "yellow"; 
  3. $fruits["watermelon"]="green"; 
  4. $founded = array_search("green", $fruits); 
  5. if($founded)  
  6.  printf("%s was founded on %s.",$founded, $fruits[$founded]) 

array_keys()函数,代码如下:

  1. $fruits["apple"] = "red"; 
  2. $fruits["banana"] = "yellow"; 
  3. $fruits["watermelon"]="green"; 
  4. $keys = array_keys($fruits); 
  5. print_r($keys); 

上面的方法都只能搜索一维数据,如果是多维数据就没办法了,php搜索多维数组的键值,如下面例子:

  1. $foo[1]['a']['xx'] = 'bar 1'; 
  2. $foo[1]['b']['xx'] = 'bar 2'; 
  3. $foo[2]['a']['bb'] = 'bar 3'; 
  4. $foo[2]['a']['yy'] = 'bar 4'; 
  5. $foo[3]['c']['dd'] = 'bar 3'; 
  6. $foo[3]['f']['gg'] = 'bar 3'; 
  7. $foo['info'][1] = 'bar 5'; 

如果要查找 bar 3 怎么进行查找呢,有三个结果,而这三个结果都要,看下面的函数:

  1. function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){ 
  2. global $nodes_found; 
  3. $a++; 
  4. foreach ($haystack as $key1=>$value1) { 
  5.     $nodes_temp[$a] = $key1; 
  6.     if (is_array($value1)){    
  7.       array_search_re($needle, $value1, $a, $nodes_temp); 
  8.     } 
  9.     else if ($value1 === $needle){ 
  10.       $nodes_found[] = $nodes_temp; 
  11.     } 
  12. return $nodes_found; 

这个函数就可以把上面要查找到的内容全部返回出键名来,代码如下:

  1. $result = array_search_re('bar 3', $foo); 
  2. print_r($result); 
  3. //输出结果为如下: 
  4. Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb ) 
  5.           [1] => Array ( [1] => 3 [2] => c [3] => dd ) 
  6.           [2] => Array ( [1] => 3 [2] => f [3] => gg ) 
  7.         ) 

php搜索多维数组的键名,代码如下:

  1. function array_search_key($needle, $haystack){ 
  2. global $nodes_found; 
  3. foreach ($haystack as $key1=>$value1) { 
  4.  
  5.  if ($key1=== $needle){ 
  6.  
  7.   $nodes_found[] = $value1; 
  8.         
  9.    } 
  10.     if (is_array($value1)){    
  11.       array_search_key($needle, $value1); 
  12.     } 
  13.     
  14.     
  15. return $nodes_found; 
  16. $result = array_search_key('a', $foo); 
  17. print_r($result); 

输出结果为如下:

  1. Array 
  2.     [0] => Array 
  3.         ( 
  4.             [xx] => bar 1 
  5.         ) 
  6.     [1] => Array 
  7.         ( 
  8.             [bb] => bar 3 
  9.         ) 
  10.     [2] => Array 
  11.         ( 
  12.             [yy] => bar 4 
  13.         ) 

通过遍历我们可以实现多维数据搜索了.

相关TAG标签
上一篇:php把数组保存数据库程序代码
下一篇:php二维数组循环程序代码
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站