Search program code for arrays in php

Source: Internet
Author: User
In php, simple data search is simple. you can use the in_array () function to search for a given value in the array. this is a simple one-dimensional data, for example, the following code: & lt ;? Php $ learn learray (& quot;

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

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

The result of executing this code: apple's color is red

Array_search () function,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])

Array_keys () function,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 );

The above methods can only search for one-dimensional data. if it is multi-dimensional data, there is no way, php searches for the key value of multi-dimensional arrays, as shown in the following 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. the code is as follows:

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

The output result is as follows:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [Xx] => bar 1
  6. )
  7. [1] => Array
  8. (
  9. [Bb] => bar 3
  10. )
  11. [2] => Array
  12. (
  13. [Yy] => bar 4
  14. )
  15. )

Multi-dimensional data search can be implemented through traversal.

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.