PHP array search (multidimensional array search)_PHP tutorial

WBOY
Release: 2016-07-13 10:44:45
Original
1109 people have browsed it

There are many ways to implement array search in php. I know that the simplest one is in_array and traversing the array and then comparing them one by one. There are other better methods. I will introduce them below.

One-dimensional array search is simple in_array()

If the value parameter is a string and the type parameter is set to true, the search is case sensitive

The code is as follows Copy code
 代码如下 复制代码

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

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

输出:

Match found

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

echo "Match found";

}

else

{

echo "Match not found";

}

?>
 代码如下 复制代码

 
$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

Output:

Match found

array_key_exists() function

If a specified key is found in an array, the function array_key_exists() returns true, otherwise it returns false. Its form is as follows:

boolean array_key_exists(mixed key,array array);
 代码如下 复制代码

 
$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.

The following example will search for apple in the array key, and if found, will output the color of the fruit:

The code is as follows Copy code

$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

 代码如下 复制代码


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

//Array ( [0] => apple [1] => banana [2] => watermelon )

array_search() function The array_search() function searches for a specified value in an array and returns the corresponding key if found, otherwise it returns false. Its form is as follows: mixed array_search(mixed needle,array haystack[,boolean strict]) The following example searches $fruits for a specific date (December 7), and if found, returns information about the corresponding state:
The code is as follows Copy code
$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() function The array_keys() function returns an array containing all keys found in the searched array. Its form is as follows: array array_keys(array array[,mixed search_value]) If the optional parameter search_value is included, only keys matching that value will be returned. The following example will output all arrays found in the $fruit array:
The code is as follows Copy code
$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() function

The array_values() function returns all the values ​​in an array and automatically provides a numerical index for the returned array. Its form is as follows:

array array_values(array array)

The following example will get the value of each element found in $fruits:

The code is as follows Copy code
 代码如下 复制代码

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

//Array ( [0] => red [1] => yellow [2] => green )


$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$values ​​= array_values($fruits);
print_r($values);

//Array ( [0] => red [1] => yellow [2] => green )

What is mentioned above is only one-dimensional array search. If we want to implement two-dimensional data or multi-dimensional data, we can refer to the following example

1 php search key value of multi-dimensional array

 代码如下 复制代码

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

As an example below:
The code is as follows Copy code
$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'; If you want to find bar 3, how to search it? There are three results, and all three results are required. Look at the following function:
-------------------------------------------------- -------------------------------------------------- ----------------------------
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;
}
-------------------------------------------------- -------------------------------------------------- --------------------------
This function can return all the key names found above
$result = array_search_re('bar 3', $foo); print_r($result); The output results are as follows:
Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb )
                                                                                                                                                                                                                         )

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
        )

)

复制代码
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);
    }
  
  
}

print_r($result); 输出结果为如下:
 
Array
(
    [0] => Array
        (
            [xx] => bar 1
        )
    [1] => Array
        (
            [bb] => bar 3
        )
    [2] => Array
        (
            [yy] => bar 4
        )
)
http://www.bkjia.com/PHPjc/633084.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633084.htmlTechArticle在php中要实现数组搜索方法有很多种,我知道最简单的就是in_array与遍历数组后再一个个对比,其它还有更多更好方法,下面我来介绍介绍。...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!