Best practise way to handle array of pairs 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 =&gt; "Name of Item 1",
          2 =&gt; "Name of Item 2",
          3 =&gt; "Name of Item 3",
          4 =&gt; "Name of Item 4",
     );

     $arrayItemPrices = array(
          1 =&gt; "$6.00",
          2 =&gt; "$4.00",
          3 =&gt; "$5.00",
          4 =&gt; "$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&lt;int, std::pair&lt;std::string, std::string&gt;&gt;, 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?
I would work with a two dimensional array, in stead of two separate arrays. You could then shuffle your array to get the items in a random order. Something like this:

$items = [
    ['id' =&gt; 1, 'name' =&gt; 'item 1', 'price' =&gt; '$1.00'],
    ['id' =&gt; 2, 'name' =&gt; 'item 2', 'price' =&gt; '$2.00'],
    ['id' =&gt; 3, 'name' =&gt; 'item 3', 'price' =&gt; '$3.00'],
    ['id' =&gt; 4, 'name' =&gt; 'item 4', 'price' =&gt; '$4.00'],
];

shuffle($items);

foreach($items as $item) {
    echo $item['id'] . ': ' . $item['name'] . ' ' . $item['price'];
    // or whatever you want to do, just trying to demonstrate you
    // won't need those $name and $price variables anymore...
}


I find it makes more sense to keep your item's properties together, in stead of spreading them over different arrays. You know when you pick an element from the items array you will have all it's properties. And to me the code is better readable as well.