3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( array("url" => "http://google.com", "priority" => 2), array("url" => "http://facebook.com", "priority" => 2), array("url" => "http://youtube.com", "priority" => 2), array("url" => "http://stackoverflow.com", "priority" => 1), array("url" => "http://kickass.to", "priority" => 1), array("url" => "http://twitter.com", "priority" => 1), array("url" => "http://example.com", "priority" => 1), ); // Recreate another array where we have multiple occurence of the same value (nb_of_occurence = priority) $listOfUrl = array(); foreach ($arr as $url) { $nbOfOccurence = $url['priority']; for($i = 0 ; $i < $nbOfOccurence ; $i++) { $listOfUrl[] = $url['url']; } } // Count the number of elements in this new array $nbOfElement = count($listOfUrl); // Generate a random index between 0 and (number of element - 1) $randomIndex = rand(0,($nbOfElement - 1)); // Retrive the random value $randomURL = $listOfUrl[$nbOfElement];

preferences:
36.63 ms | 402 KiB | 5 Q