Php Diff 2阵列并删除缺少的元素

Php Diff 2阵列并删除缺少的元素,php,arrays,match,element,array-unset,Php,Arrays,Match,Element,Array Unset,我尝试用相同元素数的较小数组中的一些信息更新较大的数组。较大的数组每24小时生成一次,较小的数组每4小时生成一次,但有时在较小的数组中,元素较少,所以我想从较大的数组中删除这些元素,但如何才能做到这一点 以下是较小数组的第一个元素: array(5) { ["category_id"]=> string(1) "8" ["product_url"]=> string(58) "http://example.net/?id=1752" ["p

我尝试用相同元素数的较小数组中的一些信息更新较大的数组。较大的数组每24小时生成一次,较小的数组每4小时生成一次,但有时在较小的数组中,元素较少,所以我想从较大的数组中删除这些元素,但如何才能做到这一点

以下是较小数组的第一个元素:

  array(5) {
    ["category_id"]=>
    string(1) "8"
    ["product_url"]=>
    string(58) "http://example.net/?id=1752"
    ["price_bgn"]=>
    float(142.8)
    ["price_eur"]=>
    float(72.99)
    ["quantity"]=>
    int(5)
  }
这是较大数组的第一个元素:

array(23) {
  ["product_id"]=>
  string(4) "1752"
  ["product_sku"]=>
  string(7) "SKU1752"
  ["category_id"]=>
  string(1) "8"
  ["product_url"]=>
  string(58) "http://example.net/?id=1752"
  ["additional_images"]=>
  array(4) {
    [0]=>
    string(64) "http://example.net/vario.jpg"
    [1]=>
    string(73) "http://example.net/duraflex_logo1.jpg"
    [2]=>
    string(67) "http://example.net/YKK-logo.jpg"
    [3]=>
    string(67) "http://example.net/Air-mesh.jpg"
  }
  ["variants"]=>
  array(4) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "4"
  }
  ["related_products"]=>
  array(4) {
    [0]=>
    array(2) {
      ["product_id"]=>
      string(2) "18"
      ["product_sku"]=>
      string(5) "SKU18"
    }
    [1]=>
    array(2) {
      ["product_id"]=>
      string(3) "248"
      ["product_sku"]=>
      string(6) "SKU248"
    }
    [2]=>
    array(2) {
      ["product_id"]=>
      string(4) "1755"
      ["product_sku"]=>
      string(7) "SKU1755"
    }
    [3]=>
    array(2) {
      ["product_id"]=>
      string(4) "1833"
      ["product_sku"]=>
      string(7) "SKU1833"
    }
  }
  ["manufacturer_id"]=>
  string(1) "1"
  ["quantity"]=>
  int(5)
  ["metadescription_bg"]=>
  string(233) ""
  ["detaileddescription_bg"]=>
  string(5342) ""
  ["metadescription_en"]=>
  string(159) ""
  ["metakeywords_en"]=>
  string(38) ""
  ["name_en"]=>
  string(38) ""
  ["price_eur"]=>
  float(72.99)
  ["shortdescription_en"]=>
  string(138) ""
  ["detaileddescription_en"]=>
  string(2485) ""
  ["images_url"]=>
  string(51) "http://example.net/?idpics=3948"
  ["images"]=>
  array(4) {
    [0]=>
    string(50) "http://example.net/pic6129.jpg"
    [1]=>
    string(50) "http://example.net/pic6164.jpg"
    [2]=>
    string(50) "http://example.net/pic6165.jpg"
    [3]=>
    string(50) "http://example.net/pic5745.jpg"
  }
}
因此,如果我匹配产品URL,我会更新价格。但若在较小的数组中并没有匹配的url,那个么如何从较大的数组中删除元素呢?这是更新的代码:

            foreach($this->productLinks as $product) {
                foreach($this->productLinksOld as $k => $productOld) {
                    if($product['product_url'] == $productOld['product_url']) {
                        $this->productLinksOld[$k]['price_bgn'] = $product['price_bgn'];
                        $this->productLinksOld[$k]['price_eur'] = $product['price_eur'];
                        $this->productLinksOld[$k]['quantity'] = $product['quantity'];
                    }
                }
            }
干杯,
乔治

我对此做了一些检查,但我不知道这是最好的方法。代码如下:

$links = unserialize(file_get_contents($this->_tempProductLinksFile));
$links[] = array(
    'category_id' => 8,
    'product_url' => 'http://www.example.net/?id=1752123',
    'price_bgn' => 1.2,
    'price_eur' => 0.6,
    'quantity' => 15,
);
$products = unserialize(file_get_contents($this->_tempFullProductInfoFile));
echo count($links).' | '.count($products).'<br />';
foreach($links as $l) {
    $ll[] = $l['product_url'];
}
foreach($products as $p) {
    $pp[] = $p['product_url'];
}
foreach($products as $k => $pm) {
    if(!in_array($pm['product_url'], $ll)) {
        echo 'This key doesn\'t exists in small array '.$k.'<br />';
    }
}
foreach($links as $k => $lm) {
    if(!in_array($lm['product_url'], $pp)) {
        echo 'This key doesn\'t exists in big array '.$k.'<br />';
    }
}
然后,我只获取其他数组中的产品url,以检查其他数组中的每个产品url,结果是:

1501 | 1503
This key doesn't exists in small array 44
This key doesn't exists in small array 313
This key doesn't exists in small array 685
This key doesn't exists in big array 1500
这意味着手动创建的链接中的最后一条记录不存在于大数组中,来自products数组的其他3个URL也不存在于小数组中。在我的旧家用机器上,该代码的运行时间约为200ms,这对我来说已经足够好了:

如果有人能给我一个更好的解决方案,我将非常感激:

干杯,乔治