3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
37.04 ms | 402 KiB | 5 Q