title: How to shuffle an array and then slice it
body: <p>I have a problem with shuffling and slicing array. </p>
  
  <p>I have this code:</p>
  
  <pre><code>$maxDisplayItem = $this-&gt;_getFlexformConfig('max_item_to_display', 'product_setting');
  $selectedProducts = array_slice($selectedProducts, 0, $maxDisplayItem);
  
  foreach ($selectedProducts as $_id) {
    shuffle($products);
    foreach ($products as $_product) {        
  ....
    }
  }
  </code></pre>
  
  <p>My code limiting the number of displayed item but didn't shuffle it at all.</p>
  
  <p>When I change the order of actions:</p>
  
  <pre><code>shuffle($selectedProducts);
  foreach ($selectedProducts as $_id) {
    $maxDisplayItem = $this-&gt;_getFlexformConfig('max_item_to_display', 'product_setting');
    $products = array_slice($products, 0, $maxDisplayItem);
    foreach ($products as $_product) {
  ....
    }
  }
  </code></pre>
  
  <p>the code shuffling and slicing results but only first (e.g. 3 results) from whole array who has 50 items.</p>
  
  <p>Could anyone help me with this?</p>
  
  <p>here is the whole function:</p>
  
  <pre><code>function displayProductList()
      {
          // Store View
          $store = $this-&gt;getStoreViewCode();
  
          $selectedProducts = $this-&gt;getSelectedProducts();
          $products = $this-&gt;_products-&gt;getProductsFromDb($selectedProducts, $store, $this-&gt;getProductsStoragePid());
  
          // Load Template File
          $templateHtml       = $this-&gt;cObj-&gt;fileResource( $this-&gt;_getConfig('templateProductList') );
          $productListHtml    = $this-&gt;cObj-&gt;getSubpart($templateHtml, '###PRODUCT_LIST###');
          $productHtml        = $this-&gt;cObj-&gt;getSubpart($productListHtml, '###PRODUCT_ITEM###');
  
          $subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';
  
          shuffle($selectedProducts);
  
          foreach ($selectedProducts as $_id) {
  
            $maxDisplayItem = $this-&gt;_getFlexformConfig('max_item_to_display', 'product_setting');
            $products = array_slice($products, 0, $maxDisplayItem);
  
              foreach ($products as $_product) {
  
                  if ($_id === $_product['product_id']) {
                      $markers = $this-&gt;_products-&gt;getProductMarkers($_product);
  
                      // Even/Odd CSS Class Determination
                      if ($even === true) {
                          $line = 'even';
                          $even = false;
                      } else {
                          $line = 'odd';
                          $even = true;
                      }
  
                      // Class Determination First/Last
                      if ($item == 0) {
                          $markers['###EVENODD###'] = $line . ' ' . 'first'; 
                      } else if ($item == $items-1) {
                          $markers['###EVENODD###'] = $line . ' ' . 'last';  
                      } else {
                          $markers['###EVENODD###'] = $line; 
                      }
  
                      // Check if the product has an image
                      $imageHtml = '&lt;p&gt;'.$this-&gt;pi_getLL('template_label_no_image_available').'&lt;/p&gt;';
                      if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
                          $imageHtml = $this-&gt;cObj-&gt;getSubpart($productHtml, '###PRODUCT_IMAGE###');
                      }
  
                      $p = $this-&gt;cObj-&gt;substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
                      $subPartContent .= $this-&gt;cObj-&gt;substituteMarkerArray($p, $markers);
                      $item++;
                  }
              }
          }
          return $this-&gt;cObj-&gt;substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
      }
  </code></pre>