Search program code for arrays in php

Source: Internet
Author: User
In php, simple data search is very simple. we can use the in_array () function to search for the given value in the array. this is a simple one-dimensional data sample code as follows: Copy the code ?...

In php, simple data search is simple. we can use the in_array () function to search for a given value in the array. this is a simple one-dimensional data.

Example

The code is as follows:

$ 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 array_key_exists () function returns true; otherwise, false. The format is as follows:


1 boolean array_key_exists (mixed key, array );

The following example searches for apple in the array key. if it is found, the color of the fruit is output:

The code is as follows:

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}

The result of executing this code is as follows:


1 apple's color is red


Array_search () function

The code is as follows:

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

The code is as follows:

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

The methods above can only search for one-dimensional data.


1. search for the key value of a multi-dimensional array in php

For example:

The code is as follows:

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

How can I find bar 3. There are three results, and all three results are required. let's look at the following function:
Bytes -------------------------------------------------------------------------------------------------------------------------------

The code is as follows:
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;
}

Else ---------------------------------------------------------------------------------------------------------------------------------
This function can return all the content to be found above to the output key name.

The code is as follows:

$ Result = array_search_re ('Bar 3', $ foo );

Print_r ($ result );

The output result 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)
)

1 php search for the key name of a multi-dimensional array

The code is as follows:

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

The output result is as follows:

Array
(
[0] => Array
(
[Xx] => bar 1
)

[1] => Array
(
[Bb] => bar 3
)

[2] => Array
(
[Yy] => bar 4
)

)

Multi-dimensional data search can be implemented through traversal.



Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.