- var_dump: documentation ( source)
- reset: documentation ( source)
- asort: documentation ( source)
- key: documentation ( source)
- rand: documentation ( source)
<?php
$products = [1, 2, 3, 4, 5];
$vendors = ["Amazon", "Newegg", "##php"];
// Super complex price calculator
function getPrice($vendor, $product) {
return rand(0, 100);
}
$productsCalculated = [];
$chosenVendors = [];
foreach ($products as $productID) {
$prices = [];
foreach ($vendors as $vendor) {
$prices[$vendor] = getPrice($vendor, $productID);
}
asort($prices);
// Save this for debugging
$productsCalculated[$productID] = $prices;
// Retreive the first item of the array as it should be lowest
reset($prices);
$chosenVendor = key($prices);
$chosenVendors[$chosenVendor][] = $productID;
}
print "Cheapest prices\n";
var_dump($chosenVendors);
print "All prices\n";
var_dump($productsCalculated);