I am trying to randomly select 4 items out of an array, each item has an index, a name and a price attached to it. Currently I have the following code segment to select 4 at random:
function SelectItems()
{
$arrayItems = array(
1 => "Name of Item 1",
2 => "Name of Item 2",
3 => "Name of Item 3",
4 => "Name of Item 4",
);
$arrayItemPrices = array(
1 => "$6.00",
2 => "$4.00",
3 => "$5.00",
4 => "$15.00",
);
$randomKeys = array_rand( $arrayItems, 4 );
foreach( $randomKeys as $key )
{
$name = $arrayItems[$key];
$price = $arrayItemPrices[$key];
/* Process item... */
}
}
However, this is not how I would approach it in C++, I would only use a std::map<int, std::pair<std::string, std::string>>
, this would map directly in PHP to an array where the value consisted of a pair of strings, however as there is no pair class in PHP I have done it with two arrays. Is this best practise in PHP?