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!
I make a little check for this but I don't know is this the best way to do this. This is the code:
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:
Then I get only product URLs in other arrays to check for each product url in other array and the result is:
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!