两个多维数组如何查询出满足如下条件的数据

朋友抬一手
  • 85
 array(2) {
   [0]=>
   array(4) {
     ["qty"]=>
     string(1) "2"
     ["county"]=>
     string(2) "ID"
     ["sku"]=>
     string(13) "B00208MM01000"
     ["id"]=>
     string(8) "50040019"
   }
   [1]=>
   array(4) {
     ["qty"]=>
     string(1) "2"
     ["county"]=>
     string(2) "ID"
     ["sku"]=>
     string(13) "B00208MM03000"
     ["id"]=>
     string(8) "50040019"
   }
 }
 

这是一个需要查询出来的数组。


array(189) {
 [0]=>
 array(4) {
   ["qty"]=>
   string(1) "1"
   ["county"]=>
   string(2) "ID"
   ["sku"]=>
   string(13) "B00208MM01000"
   ["id"]=>
   string(4) "1040"
 }
 [1]=>
 array(4) {
   ["qty"]=>
   string(1) "1"
   ["county"]=>
   string(2) "ID"
   ["sku"]=>
   string(13) "B00208MM02000"
   ["id"]=>
   string(4) "1041"
 }
 [2]=>
 array(4) {
   ["qty"]=>
   string(1) "1"
   ["county"]=>
   string(2) "ID"
   ["sku"]=>
   string(13) "B00208MM03000"
   ["id"]=>
   string(4) "1042"
 }
 [3]=>
 array(4) {
   ["qty"]=>
   string(1) "1"
   ["county"]=>
   string(2) "ID"
   ["sku"]=>
   string(13) "B00208MM01000"
   ["id"]=>
   string(4) "1043"
 }
 [4]=>
 array(4) {
   ["qty"]=>
   string(1) "1"
   ["county"]=>
   string(2) "ID"
   ["sku"]=>
   string(13) "B00208MM02000"
   ["id"]=>
   string(4) "1044"
 }
 [5]=>
 array(4) {
   ["qty"]=>
   string(1) "1"
   ["county"]=>
   string(2) "ID"
   ["sku"]=>
   string(13) "B00208MM02000"
   ["id"]=>
   string(4) "1045"
 }
 [6]=>
 array(4) {
   ["qty"]=>
   string(1) "1"
   ["county"]=>
   string(2) "ID"
   ["sku"]=>
   string(13) "B00208MM02000"
   ["id"]=>
   string(4) "1046"
 }
 

这是一个总的结果集合。
我需要从第一个数组为条件,查询出第二个总合集中符合qty,county sku相同的值,请问有人知道怎么写的吗?

   foreach ($集合1as $k=>$v){
            foreach ($集合二 as $k2 =>$v2){
                if ($v['qty']==$v2['qty'] && $v['county']==$v2['county'] && $v['sku']==$v2['sku']){
                    $data[$k2][]=$v2['id'];
                }else{
                    //unset($data[$k2]);
                }
            }

        }
        

这是我自己写的,不知道是不是哪里错了,获取的值也不对

回复
阅读 1.6k
2 个回答

刚刚看错了,array_intersect只会返回有交集的键,直接这样就好了:

foreach ($arr1 as $k=>$v){
    foreach ($arr2 as $k2=>$v2){
        if ($v['qty']==$v2['qty'] && $v['county']==$v2['county'] && $v['sku']==$v2['sku']){
            $data=$v2;
        }
    }
}

你给出的示例数据不对吧? 第一个数组 qty都是2,第二个数组里面qty都是1,怎么能有相等的?


参考实现:

$arr1 = [
    ['qty' => '2', 'country' => 'ID', 'sku' => 'B00208MM01000', 'id' => '50040019'],
    ['qty' => '2', 'country' => 'ID', 'sku' => 'B00208MM03000', 'id' => '50040019']    
];
$arr2 = [
    ['qty' => '1', 'country' => 'ID', 'sku' => 'B00208MM01000', 'id' => '1040'],
    ['qty' => '1', 'country' => 'ID', 'sku' => 'B00208MM02000', 'id' => '1041'],
    ['qty' => '1', 'country' => 'ID', 'sku' => 'B00208MM03000', 'id' => '1042'],
    ['qty' => '1', 'country' => 'ID', 'sku' => 'B00208MM01000', 'id' => '1043'],
    ['qty' => '1', 'country' => 'ID', 'sku' => 'B00208MM02000', 'id' => '1044']
];

$finalArr = [];
foreach ($arr1 as $k => $v) {
    foreach ($arr2 as $k2 => $v2) {
        if ($v2['qty'] == $v['qty'] && $v2['country'] == $v['country'] && $v2['sku'] == $v['sku']) {
            array_push($finalArr, $v2);
        }
    }
}
print_r($finalArr);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏