从数组中搜索并匹配

发布于 2024-09-27 03:29:45 字数 1605 浏览 1 评论 0原文

搜索数组 - 我发现下面这个函数可以搜索一个对于单个字符串来说“很棒”的数组,它将匹配数组值和/或数组值以及数组中的匹配键,但我真正想做的只是匹配满足/返回所有搜索值的数组。查询示例可以返回与 search1 或 serach2 匹配的任何记录的匹配项,而不是同时满足“搜索”术语 search1 和 serach2 的记录。

示例数组,

array 1 - key1=>search1,key2=>search2,key3=>search3,key4=>search4
array 2 - key1=>search2,key2=>search1,key3=>search4,key4=>search2
array 3 - key1=>search3,key2=>search2,key3=>search4,key4=>search1
array 4 - key1=>search1,key2=>search3,key3=>search2,key4=>search4

因此如果您为这些数组运行函数(上面),它应该只匹配 array1,因为只有 array1 的搜索和键都匹配。尽管 array4 确实与 key1 的搜索匹配,但不应返回,因为它与 key2 的搜索不匹配,就像 array3 一样,因为它仅与 key2 匹配。

建议与建议请帮忙。

function recursiveArraySearch($haystack, $needle, $index = null) {
   $aIt   = new RecursiveArrayIterator($haystack);
   $it    = new RecursiveIteratorIterator($aIt);
   while($it->valid()) {       
      if(((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
         return $aIt->key();
      }

      $it->next();
   }

   return false;
}



$sql = mysql_query("SELECT * FROM atable WHERE tableID = '1' ");
while($row = mysql_fetch_array($sql)):
    $content = json_decode($row['tosearch']);
    echo recursiveArraySearch(json_decode($row['tosearch']), 'search1','key1');    
    echo recursiveArraySearch(json_decode($row['tosearch']), 'search2','key2'); 
endwhile;

该函数的“理想”格式如下所示:

echo recursiveArraySearch(json_decode($row['tosearch']),
array('search1'=>'key1'),array(('search2'=>'key2'));  

Search an array - I found this function below to search an array which is "great" for single strings, it will match an array value and or array value and matching key within an array, BUT what i really want to do is ONLY match an array where ALL the search values are met/returned. the query example could return matches for any records where there is a match for either - search1 or serach2 NOT where both "search" terms - search1 and serach2 - are met.

example arrays

array 1 - key1=>search1,key2=>search2,key3=>search3,key4=>search4
array 2 - key1=>search2,key2=>search1,key3=>search4,key4=>search2
array 3 - key1=>search3,key2=>search2,key3=>search4,key4=>search1
array 4 - key1=>search1,key2=>search3,key3=>search2,key4=>search4

so if you ran the function for these arrays (above) It should ONLY match array1 as only array1 has both search and key matched. Although array4 does match the search for key1 it should not be returned as it does not match the search for key2, like wise array3 as this only matches on key2.

Suggestions & help please.

function recursiveArraySearch($haystack, $needle, $index = null) {
   $aIt   = new RecursiveArrayIterator($haystack);
   $it    = new RecursiveIteratorIterator($aIt);
   while($it->valid()) {       
      if(((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
         return $aIt->key();
      }

      $it->next();
   }

   return false;
}



$sql = mysql_query("SELECT * FROM atable WHERE tableID = '1' ");
while($row = mysql_fetch_array($sql)):
    $content = json_decode($row['tosearch']);
    echo recursiveArraySearch(json_decode($row['tosearch']), 'search1','key1');    
    echo recursiveArraySearch(json_decode($row['tosearch']), 'search2','key2'); 
endwhile;

The "ideal" format for the function would be something like this:

echo recursiveArraySearch(json_decode($row['tosearch']),
array('search1'=>'key1'),array(('search2'=>'key2'));  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2024-10-04 03:29:45

我不明白一切,但我认为它可能有用: http://php .net/manual/en/language.operators.array.php

I don't understand everything but I think it's could be useful : http://php.net/manual/en/language.operators.array.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文