首页 >php教程 >php手册 >php中数组的搜索程序代码

php中数组的搜索程序代码

WBOY
WBOY原创
2016-05-25 16:55:201187浏览
在php中简单数据搜索很简单我们直接使用in_array() 函数在数组中搜索给定的值即可,这种是简单的一维数据

 代码如下 复制代码
 代码如下 复制代码

$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn",$people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>

$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn",$people))

  {

  echo "Match found";
  }

else

  {
  echo "Match not found";

  }

?>

输出:

 代码如下 复制代码

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 }

Match found。

array_key_exists()函数

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

1 boolean array_key_exists(mixed key,array array);

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

$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])

 

 代码如下 复制代码
 代码如下 复制代码

$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$keys = array_keys($fruits);
print_r($keys);

1 $fruit["apple"] = "red"; 

2 $fruit["banana"] = "yellow"; 


3 $fruit["pear"] = "green"; 

4 if(array_key_exists("apple", $fruit)){ 

 代码如下 复制代码

$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';

5     printf("apple's color is %s",$fruit["apple"]); 6 }
执行这段代码得到的结果:1 apple's color is redarray_search()函数
 代码如下 复制代码
$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])
array_keys()函数
 代码如下 复制代码
$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$keys = array_keys($fruits);print_r($keys);
上面的方法都只能搜索一维数据,如果是多维数据就没办法了。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 怎么进行查找呢。有三个结果,而这三个结果都要,看下面的函数:
-------------------------------------------------------------------------------------------------------------------------------

      $nodes_found[] = $nodes_temp;
    }}
return $nodes_found;
 代码如下
 代码如下 复制代码
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;
}
复制代码


function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){global $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 )
        )

$a ;

foreach ($haystack as $key1=>$value1) {

    $nodes_temp[$a] = $key1;
 代码如下 复制代码

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
        )

)

    if (is_array($value1)){  

      array_search_re($needle, $value1, $a, $nodes_temp);

    }

    else if ($value1 === $needle){

}

---------------------------------------------------------------------------------------------------------------------------------

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

 代码如下 复制代码
$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 )        ) 1 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        )) 通过遍历我们可以实现多维数据搜索了。 教程链接:随意转载~但请保留教程地址★
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn