поиск многомерного массива php по нескольким ключам и значениям - PullRequest
0 голосов
/ 31 мая 2019

У меня есть многомерный массив 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 поиск многомерного массива по значению

Эта функция хорошо работает для двух пар ключ-значение и возвращает индекс массива. Но я хочупоиск по нескольким ключам и паре 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
        )

)

1 Ответ

0 голосов
/ 31 мая 2019

Вы можете создать ассоциативный массив и проверить, существуют ли все ключи / значения в этом ассоциативном массиве в основном массиве

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;

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...