A key-value program for searching multidimensional arrays in PHP

Source: Internet
Author: User
Tags arrays

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're looking for bar 3, how do I find it? There are three results, and these three results are to look at the following function:
--------------------------------------------------------------------------------------------------------------- ----------------

  code is as follows copy code
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 can return all the contents of the above search to the key name

The code is as follows Copy Code

$result = Array_search_re (' Bar 3 ', $foo);

Print_r ($result);

The output results are 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 multidimensional array key name

  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 _fo und[] = $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 results are as follows:

The code is as follows Copy Code

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

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

[2] => Array
(
[yy] => Bar 4
)

)

Similar to full text matching! Two loops first loop keys second fuzzy match with function strstr () to solve

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.