如何对数组进行洗牌然后对其进行切片

时间:2013-07-19 12:16:31

标签: php shuffle slice

我有洗牌和切片数组的问题。

我有这段代码:

$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$selectedProducts = array_slice($selectedProducts, 0, $maxDisplayItem);

foreach ($selectedProducts as $_id) {
  shuffle($products);
  foreach ($products as $_product) {        
....
  }
}

我的代码限制了显示的项目数量,但根本没有随机播放。

当我改变行动的顺序时:

shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
  $maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
  $products = array_slice($products, 0, $maxDisplayItem);
  foreach ($products as $_product) {
....
  }
}

代码改组和切片结果但只有第一个(例如3个结果)来自整个数组,有50个项目。

有人可以帮我吗?

这是整个功能:

function displayProductList()
    {
        // Store View
        $store = $this->getStoreViewCode();

        $selectedProducts = $this->getSelectedProducts();
        $products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());

        // Load Template File
        $templateHtml       = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
        $productListHtml    = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
        $productHtml        = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');

        $subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';

        shuffle($selectedProducts);

        foreach ($selectedProducts as $_id) {

          $maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
          $products = array_slice($products, 0, $maxDisplayItem);

            foreach ($products as $_product) {

                if ($_id === $_product['product_id']) {
                    $markers = $this->_products->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 = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
                    if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
                        $imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
                    }

                    $p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
                    $subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
                    $item++;
                }
            }
        }
        return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
    }

1 个答案:

答案 0 :(得分:0)

function displayProductList(){
    // Store View
    $store = $this->getStoreViewCode();

    $selectedProducts = $this->getSelectedProducts();
    $products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());

    // Load Template File
    $templateHtml       = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
    $productListHtml    = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
    $productHtml        = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');

    $subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';

    $maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
    $products = array_slice($products, 0, $maxDisplayItem);

    shuffle($products);

    foreach ($products as $_product) {

        $markers = $this->_products->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 = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
        if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
            $imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
        }

        $p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
        $subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
        $item++;

    }

    return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}