Php array search (multi-dimensional array search)

Source: Internet
Author: User
There are many ways to implement array search in php. I know that the simplest thing is to compare in_array with the array traversal. there are more and better methods. I will introduce them below. One-dimensional data

There are many ways to implement array search in php. I know that the simplest thing is to compare in_array with the array traversal. there are more and 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 area is case sensitive. the code is as follows:

  1. $ People = array ("Peter", "Joe", "Glenn", "Cleveland ");
  2. If (in_array ("Glenn", $ people ))
  3. {
  4. Echo "Match found ";
  5. }
  6. Else
  7. {
  8. Echo "Match not found ";
  9. }
  10. ?>

Output: Match found

Array_key_exists () function

If a specified key is found in an array, the array_key_exists () function returns true; otherwise, the function returns false in the following format:

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. }
  7. // Apple's color is red

Array_search () function

The array_search () function searches for a specified value in an array. if it is found, the corresponding key is returned. otherwise, false is returned. the format is as follows:

Mixed array_search (mixed needle, array haystack [, boolean strict])

The following example searches for a specific date (December 7) in $ fruits. if it is found, the related information is returned. the code is as follows:

  1. $ Fruits ["apple"] = "red ";
  2. $ Fruits ["banana"] = "yellow ";
  3. $ Fruits ["watermelon"] = "green ";
  4. $ Founded = array_search ("green", $ fruits );
  5. If ($ founded)
  6. Printf ("% s was founded on % s.", $ founded, $ fruits [$ founded]);
  7. // Watermelon was founded on green.

Array_keys () function

The array_keys () function returns an array containing all the keys found in the searched array. the format is as follows:

Array array_keys (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:

  1. $ Fruits ["apple"] = "red ";
  2. $ Fruits ["banana"] = "yellow ";
  3. $ Fruits ["watermelon"] = "green ";
  4. $ Keys = array_keys ($ fruits );
  5. Print_r ($ keys );
  6. // Array ([0] => apple [1] => banana [2] => watermelon)

Array_values () function

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

Array array_values (array)

The following example gets the values of each element found in $ fruits. the code is as follows:

  1. $ Fruits ["apple"] = "red ";
  2. $ Fruits ["banana"] = "yellow ";
  3. $ Fruits ["watermelon"] = "green ";
  4. $ Values = array_values ($ fruits );
  5. Print_r ($ values );
  6. // Array ([0] => red [1] => yellow [2] => green)

All of the above is a one-dimensional array search. if you want to implement two-dimensional data or multi-dimensional data, we can refer to the following example.

Php searches for the key value of a multi-dimensional array,For example:

  1. $ Foo [1] ['A'] ['XX'] = 'bar 1 ';
  2. $ Foo [1] ['B'] ['XX'] = 'bar 2 ';
  3. $ Foo [2] ['A'] ['BB '] = 'bar 3 ';
  4. $ Foo [2] ['A'] ['yy'] = 'bar 4 ';
  5. $ Foo [3] ['c'] ['DD'] = 'bar 3 ';
  6. $ Foo [3] ['F'] ['GG '] = 'bar 3 ';
  7. $ Foo ['info'] [1] = 'bar 5 ';

If you want to find bar 3, how can you find it? there are three results, and all three results are required. See the following function:

  1. Function array_search_re ($ needle, $ haystack, $ a = 0, $ nodes_temp = array ()){
  2. Global $ nodes_found;
  3. $ A ++;
  4. Foreach ($ haystack as $ key1 => $ value1 ){
  5. $ Nodes_temp [$ a] = $ key1;
  6. If (is_array ($ value1 )){
  7. Array_search_re ($ needle, $ value1, $ a, $ nodes_temp );
  8. }
  9. Else if ($ value1 ===$ needle ){
  10. $ Nodes_found [] = $ nodes_temp;
  11. }
  12. }
  13. Return $ nodes_found;
  14. }

This function returns the output key name for all the content to be searched.

  1. $ Result = array_search_re ('Bar 3', $ foo );
  2. Print_r ($ result );
  3. // The output result is as follows:
  4. Array ([0] => Array ([1] => 2 [2] => a [3] => bb)
  5. [1] => Array ([1] => 3 [2] => c [3] => dd)
  6. [2] => Array ([1] => 3 [2] => f [3] => gg)
  7. )

Php searches for the key name of a multi-dimensional array. the code is as follows:

  1. Function array_search_key ($ needle, $ haystack ){
  2. Global $ nodes_found;
  3. Foreach ($ haystack as $ key1 => $ value1 ){
  4.  
  5. If ($ key1 ===$ needle ){
  6.  
  7. $ Nodes_found [] = $ value1;
  8. }
  9. If (is_array ($ value1 )){
  10. Array_search_key ($ needle, $ value1 );
  11. }
  12. }
  13. Return $ nodes_found;
  14. }
  15. $ Result = array_search_key ('A', $ foo );
  16. Print_r ($ result );
  17. // The output result is as follows:
  18. Array
  19. (
  20. [0] => Array
  21. (
  22. [Xx] => bar 1
  23. )
  24. [1] => Array
  25. (
  26. [Bb] => bar 3
  27. )
  28. [2] => Array
  29. (
  30. [Yy] => bar 4
  31. )
  32. )

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.