Search for multi-dimensional array key-value programs in php-PHP source code

Source: Internet
Author: User
For example, if you search for "OK", it will match the array "Hello, good, good" in "OK". If "OK" does not match the first array, fuzzy search for all the keywords that contain "good", such as "Hello, good, and hobby". For example, search for "OK" will match the array in "OK". "Hello, good, good \ "printed out if the search" good "does not match the first array, it will fuzzy search for all the keywords containing" good ", such as" Hello, good hobby"

Script ec (2); script

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:

The Code is as follows:

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

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

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

)

Similar to full-text match! Use two loops, the first loop, the keys, the second Fuzzy match, and use the strstr () function to solve the problem.

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.