首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Array / Foreach循环中的下一个和最后一个元素返回一个键

从Array / Foreach循环中的下一个和最后一个元素返回一个键
EN

Stack Overflow用户
提问于 2016-06-03 00:20:45
回答 1查看 82关注 0票数 0

我希望修改eCommerce平台(基于OpenCart- PHP )显示折扣的方式。

默认行为是折扣将显示为:

  • 5美元或以上:20美元
  • 10美元或以上:18.00美元
  • 20美元或以上:16.00美元

我更希望:

  • 5- 9: 20美元
  • 10-19:18.00美元
  • 20+:16.00美元

通过模板文件(下面提供的代码)去掉“或更多”文本非常简单。

对于除最后一个元素以外的所有元素,这将要求从下一个元素中获取quantity键($折扣‘quantity’),并应用一个基本的数学函数(- 1),然后返回这个除原始值之外的新值。

对于最后一个元素,我只需返回最后一个quantity值并添加"+“文本。

原始代码(控制器):

代码语言:javascript
复制
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);

$this->data['discounts'] = array(); 

foreach ($discounts as $discount) {
    $this->data['discounts'][] = array(
        'quantity' => $discount['quantity'],
        'price'    => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
    );
}

原始代码(模板):

代码语言:javascript
复制
<?php if ($discounts) { ?>
    <div class="discount">
        <?php foreach ($discounts as $discount) { ?>
            <span><?php echo sprintf($text_discount, $discount['quantity'], $discount['price']); ?></span>
        <?php } ?>
    </div>
<?php } ?>

从模板中删除“或更多”文本的修改代码(注意:单独的echo用于允许表格式-为了保持简单性,这些标记不包括在内):

代码语言:javascript
复制
<?php if ($discounts) { ?>
    <div class="discount">
        <?php foreach ($discounts as $discount) { ?>
            <?php echo $discount['quantity']; ?><?php echo $discount['price']; ?>
        <?php } ?>
    </div>
<?php } ?>

如何进一步修改此代码以返回首选格式的数量?

注意:数组很小,但我仍然认为性能是优先考虑的。

编辑:

谢谢您提供下面的解决方案。这是我在模板文件中使用的用于自定义表格式的代码(没有sprintf/格式化字符串函数)。

代码语言:javascript
复制
<?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
<tr>
  <td><?php echo $discounts[$i]['quantity']; ?> - <?php echo (int)$discounts[$i+1]['quantity'] - 1; ?></td>
  <td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
<?php if (count($discounts)) { ?>
<tr>
  <td><?php echo $discounts[$i]['quantity']; ?>+</td>
  <td><?php echo $discounts[$i]['price']; ?></td>
</tr>
<?php } ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-03 00:52:19

您可以在product.php控制器文件中尝试这一点,非常简单。

代码语言:javascript
复制
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);

$discounts_formated = array();

for ($i=0; $i < count($discounts) -1; $i++) { 
    $discounts_formated[] =  sprintf("%s - %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1);
}

// Last discount: 30+
$discounts_formated[] =  sprintf("%s+", $discounts[$i]['quantity']);

var_dump($discounts_formated);

输出:

代码语言:javascript
复制
array (size=3)
  0 => string '10 - 19' (length=7)
  1 => string '20 - 29' (length=7)
  2 => string '30+' (length=3)

编辑:

编辑文件product.tpl,我用OC1.5.6.4进行了测试,它正在工作

代码语言:javascript
复制
<?php if ($discounts) { ?>
    <br />
    <div class="discount">
        <?php for ($i=0; $i < count($discounts) -1; $i++) { ?>
            <?php echo sprintf("%s - %s: %s", $discounts[$i]['quantity'], (int)$discounts[$i+1]['quantity'] - 1, $discounts[$i]['price']); ?><br />
        <?php } ?>
        <?php if (count($discounts)) { // last discount ?>
            <?php echo sprintf("%s+: %s", $discounts[$i]['quantity'],  $discounts[$i]['price']); ?><br />
        <?php } ?>
    </div>
<?php } ?>

会打印这样的东西:

代码语言:javascript
复制
10 - 19: $88.00
20 - 29: $77.00
30+: $66.00
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37604111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档