我有一个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和5进行搜索
我想要一个类似
的函数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
)
)
答案 0 :(得分:0)
您可以创建一个关联数组,并测试该关联数组中的所有键/值是否都存在于主数组中
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;
}
答案 1 :(得分:0)
对于“活着死”评论,请使用数组作为条件,其中条件是键中要检查的键,值是要在这些键中搜索的值。
首先定义将1个元素检查为的函数:
function checkElemet($element, $conditions) {
foreach($conditions as $k => $v) {
if (isset($element[$k]) && $element[$k] != $v)
return false; // if both set and not equal don't need keep checking
}
return true;
}
然后在数组上循环并检查:
function search_by_value($array, $conditions) {
foreach($array as $k => $v)
if (checkElemet($v, $conditions)) $res[] = $k;
return $res;
}
实时示例:3v4l