PHP Array Search (multidimensional array search) _php tutorial

Source: Internet
Author: User
In PHP to implement the array search method There are many, I know the simplest is in_array and iterate after the array and then a comparison, there are more and better ways, let me introduce.

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";
}
?>

Output:

Match found


Array_key_exists () function

If a specified key is found in an array, the function array_key_exists () returns True, otherwise false is returned. The form is as follows:

Boolean array_key_exists (mixed Key,array array);

The following example searches for Apple in the array key and, if found, outputs 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

Array_search () function

The Array_search () function searches for a specified value in an array, returns the corresponding key if found, otherwise returns false. The form is as follows:

Mixed Array_search (mixed Needle,array Haystack[,boolean Strict])

The following example searches for a specific date in $fruits (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 is founded on%s.", $founded, $fruits [$founded]);

Watermelon was founded on green.

Array_keys () function

The Array_keys () function returns an array that contains all the keys found in the searched array. The form is as follows:

Array Array_keys (array array[,mixed search_value])


If the optional parameter search_value is included, only the key that matches the value is returned. The following example outputs all the 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 numeric index for the returned array. The form is as follows:

Array array_values (array array)

The following example gets the values 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)

The above is only one-dimensional array search, if you want to implement two-dimensional data or multidimensional data we can refer to the following examples

1 PHP searches for a multidimensional array of key values

As in the following example:

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 find it. There are three results, and these three results are all, look at the following function:
--------------------------------------------------------------------------------------------------------------- ----------------
function Array_search_re ($needle, $haystack, $a =0, $nodes _temp=array ()) {
Global $nodes _found;
$a + +;
foreach ($haystack as $key 1=> $value 1) {
$nodes _temp[$a] = $key 1;
if (Is_array ($value 1)) {
Array_search_re ($needle, $value 1, $a, $nodes _temp);
}
else if ($value 1 = = = $needle) {
$nodes _found[] = $nodes _temp;
}
}
return $nodes _found;
}
--------------------------------------------------------------------------------------------------------------- ------------------
This function will return all the contents of the above search to the key name to
$result = Array_search_re (' Bar 3 ', $foo);

Print_r ($result);

The output is as follows:
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 searches for the key names of multidimensional arrays

The code is as follows Copy Code

function Array_search_key ($needle, $haystack) {
Global $nodes _found;

foreach ($haystack as $key 1=> $value 1) {

if ($key 1=== $needle) {

$nodes _found[] = $value 1;

}
if (Is_array ($value 1)) {
Array_search_key ($needle, $value 1);
}


}

return $nodes _found;
}
$result = Array_search_key (' A ', $foo);

Print_r ($result);

The output is as follows:

Array
(
[0] = = Array
(
[XX] = bar 1
)

[1] = = Array
(
[BB] = Bar 3
)

[2] = = Array
(
[YY] = Bar 4
)

)

http://www.bkjia.com/PHPjc/633084.html www.bkjia.com true http://www.bkjia.com/PHPjc/633084.html techarticle in PHP to implement the array search method There are many, I know the simplest is in_array and iterate after the array and then a comparison, there are more and better ways, let me introduce. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.