php多维数组通过多个键和值搜索

Posted

技术标签:

【中文标题】php多维数组通过多个键和值搜索【英文标题】:php multidimensional array search by multiple key and value 【发布时间】:2019-10-16 21:00:25 【问题描述】:

我有一个 php 多维数组

 Array
(
  [0] => Array
    (
        [id] => 128
        [form_id] => 5
        [post_id] => 
        [date_created] => 2019-05-27 10:25:59
        [date_updated] => 2019-05-27 10:25:59
        [is_starred] => 0
        [is_read] => 0
        [ip] => 10.10.10.1
        [source_url] => https://nate.rp.cisinlive.com/?gf_page=preview&id=5
        [user_agent] => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/54.0.2840.100 Safari/537.36
        [currency] => USD
        [payment_status] => 
        [payment_date] => 
        [payment_amount] => 
        [payment_method] => 
        [transaction_id] => 
        [is_fulfilled] => 
        [created_by] => 1
        [transaction_type] => 
        [status] => active
        [54] => family
        [59] => 24
        [69] => 24
        [75] => YToyOntpOjA7YToyOntzOjEwOiJwcm9kdWN0X2lkIjtzOjI6IjM2IjtzOjE2OiJwcm9kdWN0X3F1YW50aXR5IjtzOjE6IjEiO31pOjE7YToyOntzOjEwOiJwcm9kdWN0X2lkIjtzOjI6IjI0IjtzOjE2OiJwcm9kdWN0X3F1YW50aXR5IjtzOjE6IjEiO319
        [66] => 2
        [67] => AZ
        [68] => Installation 1
        [71] => apoorv
        [72] => kanungo
        [79] => Lead CYPC
        [82] => (898) 989-8989
        [83] => 89
        [78] => apoorv.k@cisinlabs.com
        [86] => Shawna Harlin-Clifton
        [87] => Army Guards Training
        [90] => 01
        [91] => 00
        [81] => United States
        [94] => test event
        [95] => 2019-05-16
        [96] => 2
        [97] => AK
        [98] => Installation 1
        [99] => Unit
        [100] => apoorv
        [101] => test
        [102] => apoorv.k@cisinlabs.com
        [103] => Lead CYPC
        [106] => United States
        [121.1] => indore
        [121.3] => indore
        [121.4] => Alaska
        [121.5] => 452001
        [121.6] => United States
        [120.1] => 1
        [120.2] => I agree to the Anchored4Life Disclaimer Sentence
        [120.3] => 2
        [58] => 
        [55] => 
        [2] => 
        [57] => 
        [63] => 
        [123] => 
        [61] => 
        [74] => 
        [108] => 
        [60] => 
        [76] => 
        [77] => 
        [124] => 
        [80] => 
        [84] => 
        [126] => 
        [128] => 
        [104] => 
        [129] => 
        [121.2] => 
        [127] => 
        [125] => 
    )

[1] => Array
    (
        [id] => 127
        [form_id] => 5
        [post_id] => 
        [date_created] => 2019-05-24 23:24:52
        [date_updated] => 2019-05-24 23:24:52
        [is_starred] => 0
        [is_read] => 0
        [ip] => 207.255.5.251
        [source_url] => https://nate.rp.cisinlive.com/family-order-2/
        [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36
        [currency] => USD
        [payment_status] => 
        [payment_date] => 
        [payment_amount] => 
        [payment_method] => 
        [transaction_id] => 
        [is_fulfilled] => 
        [created_by] => 1
        [transaction_type] => 
        [status] => active
        [54] => family
        [59] => 24
        [69] => 24
        [75] => YToxOntpOjA7YToyOntzOjEwOiJwcm9kdWN0X2lkIjtzOjI6IjM2IjtzOjE2OiJwcm9kdWN0X3F1YW50aXR5IjtzOjI6IjU0Ijt9fQ==
        [67] => AL
        [68] => Installation 2
        [71] => Nate
        [72] => Seidel
        [79] => Other
        [82] => (234) 343-2432
        [78] => nate@simcloud.com
        [81] => United States
        [121.6] => United States
        [58] => 
        [55] => 
        [2] => 
        [57] => 
        [63] => 
        [123] => 
        [61] => 
        [74] => 
        [108] => 
        [60] => 
        [76] => 
        [77] => 
        [124] => 
        [66] => 
        [80] => 
        [83] => 
        [84] => 
        [86] => 
        [87] => 
        [90] => 
        [91] => 
        [126] => 
        [128] => 
        [94] => 
        [95] => 
        [96] => 
        [97] => 
        [98] => 
        [99] => 
        [100] => 
        [101] => 
        [102] => 
        [103] => 
        [104] => 
        [106] => 
        [129] => 
        [121.1] => 
        [121.2] => 
        [121.3] => 
        [121.4] => 
        [121.5] => 
        [127] => 
        [120.1] => 
        [120.2] => 
        [120.3] => 
        [125] => 
    )

[2] => Array
    (
        [id] => 126
        [form_id] => 5
        [post_id] => 
        [date_created] => 2019-05-24 09:58:35
        [date_updated] => 2019-05-24 09:58:35
        [is_starred] => 0
        [is_read] => 0
        [ip] => 10.10.10.1
        [source_url] => https://nate.rp.cisinlive.com/?gf_page=preview&id=5
        [user_agent] => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36
        [currency] => USD
        [payment_status] => 
        [payment_date] => 
        [payment_amount] => 
        [payment_method] => 
        [transaction_id] => 
        [is_fulfilled] => 
        [created_by] => 1
        [transaction_type] => 
        [status] => active
        [54] => family
        [59] => 24
        [69] => 24
        [75] => YToyOntpOjA7YToyOntzOjEwOiJwcm9kdWN0X2lkIjtzOjI6IjM2IjtzOjE2OiJwcm9kdWN0X3F1YW50aXR5IjtzOjE6IjEiO31pOjE7YToyOntzOjEwOiJwcm9kdWN0X2lkIjtzOjI6IjI0IjtzOjE2OiJwcm9kdWN0X3F1YW50aXR5IjtzOjE6IjEiO319
        [66] => 2
        [67] => AK
        [68] => Installation 2
        [71] => apoorv
        [72] => test
        [79] => Lead CYPC
        [82] => (898) 998-9898
        [83] => 898989
        [84] => (898) 989-8989
        [78] => apoorv.k@cisinlabs.com
        [86] => Shawna Harlin-Clifton
        [87] => Army Guards Training
        [90] => 00
        [91] => 00
        [94] => test
        [95] => 2019-05-23
        [96] => 2
        [97] => AL
        [98] => Installation 1
        [99] => Unit
        [100] => apoorv
        [101] => test
        [102] => apoorv.k@cisinlabs.com
        [103] => Lead CYPC
        [106] => United States
        [121.1] => indore
        [121.3] => indore
        [121.5] => 452001
        [121.6] => United States
        [120.1] => 1
        [120.2] => I agree to the Anchored4Life Disclaimer Sentence
        [120.3] => 2
        [58] => 
        [55] => 
        [2] => 
        [57] => 
        [63] => 
        [123] => 
        [61] => 
        [74] => 
        [108] => 
        [60] => 
        [76] => 
        [77] => 
        [124] => 
        [80] => 
        [81] => 
        [126] => 
        [128] => 
        [104] => 
        [129] => 
        [121.2] => 
        [121.4] => 
        [127] => 
        [125] => 
    ) )

我想使用多个键和对在这个数组中搜索

目前,我正在使用该功能

function search_revisions($dataArray, $search_value, $key_to_search, $other_matching_value = null, $other_matching_key = null) 
    // This function will search the revisions for a certain value
    // related to the associative key you are looking for.
    $keys = array();
    foreach ($dataArray as $key => $cur_value) 
        if ($cur_value[$key_to_search] == $search_value) 
            if (isset($other_matching_key) && isset($other_matching_value)) 
                if ($cur_value[$other_matching_key] == $other_matching_value) 
                    $keys[] = $key;
                
             else 
                // I must keep in mind that some searches may have multiple
                // matches and others would not, so leave it open with no continues.
                $keys[] = $key;
            
        
    
    return $keys;

从@amurrell 那里借来回答PHP multidimensional array search by value

此函数适用于两个键值对并返回数组的索引但我想通过多个键和对 4 甚至五个键和值对进行搜索

我想要一个类似的功能

search_by_value($array,$first_key,$first_value,$second_key=null,$second_value=null,$third_key=null,$third_value=null,$fourth_key=null,$fourth_value=null);

或者更像

search_by_value(array('$key',$value));

并且输出应该是匹配键值的索引

    Array
(
    [0] => 5
)

或者如果它匹配多个值

    Array
(
    [0] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

)

【问题讨论】:

不是传递多个key and there corresponding values 参数,而是传递一个数组参数,其中包含这样的值:- array('search key'=>'search value','search key2'=>'search value2') 这样 【参考方案1】:

您可以创建一个关联数组并测试该关联数组中的所有键/值是否都存在于主数组中

function search_by_values($array,$searchValues) 
$keys = [];
foreach($array as $key => $value) //loop each array value
      $isInArray = false;//
      foreach($searchValues as $searchKey => $searchValue) //loop the search array
         if($value[$searchKey] != $searchValue) //test if the key value pair is in the array
              $isInArray = false;//the value of the key doesn't match 
               break;//we exit the inner loop with  $isInArray = false
          else 
            $isInArray = true;//if all the values are in the array we will exit with  $isInArray = true;
         
      
      if($isInArray) 
        $keys[] = $key;
      

return $keys;


【讨论】:

以上是关于php多维数组通过多个键和值搜索的主要内容,如果未能解决你的问题,请参考以下文章

Laravel PHP通过多个键值过滤多维数组

通过在键和值中搜索部分匹配来过滤数组元素

php 多个多维数组求交集

PHP中数组的定义及声明实例

PHP多维数组搜索并获取键的数组[重复]

使用带有 sql 的键值对搜索 php 多维关联数组,例如 '%LIKE%' 构造