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

时间:2022-07-01 15:39:57

I have a problem with shuffling and slicing array.

我有一个洗牌和切片阵列的问题。

I have this code:

我有这个代码:

$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) {        
....
  }
}

My code limiting the number of displayed item but didn't shuffle it at all.

我的代码限制了显示项目的数量,但根本没有洗牌。

When I change the order of actions:

当我改变动作的顺序时:

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) {
....
  }
}

the code shuffling and slicing results but only first (e.g. 3 results) from whole array who has 50 items.

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

Could anyone help me with this?

任何人都可以帮我这个吗?

here is the whole function:

这是整个功能:

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 个解决方案

#1


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);
}

#1


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);
}