Search code examples
phparraysmatchelementarray-unset

Diff 2 arrays and remove missing elements


I try to update bigger array with some info from smaller array with same number of elements. Bigger array is generated every 24 hours but the smaller is generated every 4 hours but sometimes in smaller array there few elements less, so I want to delete these elements from bigger array but how I can do that?

Here is a first element of smaller array:

  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)
  }

Here is a first element of bigger array:

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"
  }
}

So if I match product urls I update prices. But how to remove element from bigger array if there is no matched url in smaller array? This is the code for update:

            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'];
                    }
                }
            }

cheers, George!


Solution

  • I make a little check for this but I don't know is this the best way to do this. This is the code:

    $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 />';
        }
    }
    

    First 1 get cached content for small array as links and for big array as products. Then I make new sub array to links to check case if in small array is a product without whole info in big array. After all I make a count for 2 arrays and the result is:

    1501 | 1503
    

    Then I get only product URLs in other arrays to check for each product url in other array and the result is:

    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
    

    Which means that last record in links (created manually) not exists in big array and 3 other urls from products array doesn't exists in small array. Also run time of this code is ~200ms on my old home machine which is good enough for me :)

    If any one can give me a better solution I'll be very grateful :)

    cheers, George!