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); } sort($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); var_dump($chosenVendor); $chosenVendors[$chosenVendor][] = $productID; } var_dump($productsCalculated); var_dump($chosenVendors);

preferences:
37.18 ms | 402 KiB | 5 Q