【问题标题】:Return a Key from the Next and Last Element in Array / Foreach Loop从 Array / Foreach 循环中的下一个和最后一个元素返回一个键
【发布时间】:2016-06-03 00:20:45
【问题描述】:

我希望修改电子商务平台的折扣显示方式(OpenCart - 基于 PHP MVC)。

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

  • 5 个或更多:$20.00
  • 10 个或更多:18.00 美元
  • 20 个或更多:$16.00

我更喜欢:

  • 5 - 9:20.00 美元
  • 10 - 19:18.00 美元
  • 20 岁以上:16.00 美元

通过模板文件剥离“或更多”文本很简单(下面提供的代码)。

对于除最后一个元素之外的所有元素,这将需要从下一个元素中获取数量键 ($discount['quantity']) 并应用基本数学函数 (- 1),然后返回除原始值之外的新值。

对于最后一个元素,我只需要返回最后一个数量值并添加“+”文本。

原始代码(控制器):

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

原始代码(模板):

<?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 用于允许表格格式 - 为了保持简单,不包括这些标签):

<?php if ($discounts) { ?>
    <div class="discount">
        <?php foreach ($discounts as $discount) { ?>
            <?php echo $discount['quantity']; ?><?php echo $discount['price']; ?>
        <?php } ?>
    </div>
<?php } ?>

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

注意:阵列非常小,但我仍会优先考虑性能。

编辑:

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

<?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 } ?>

【问题讨论】:

  • PHP 有 nextcurrentprevlast 数组函数

标签: php arrays opencart


【解决方案1】:

你可以试试这个,很简单,在product.php控制器文件中

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

输出:

array (size=3)
  0 => string '10 - 19' (length=7)
  1 => string '20 - 29' (length=7)
  2 => string '30+' (length=3)

编辑:

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

<?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 } ?>

将打印如下内容:

10 - 19: $88.00
20 - 29: $77.00
30+: $66.00

【讨论】:

  • 谢谢。将尽快尝试并报告。
  • 不幸的是我没有成功。您是否有机会进一步澄清应该对我提供的代码进行哪些更改?
  • 谢谢您,它运行良好!我一直想学习递增/递减运算符。虽然,我稍微修改了自定义表格格式的代码(我将把它添加到我的原始帖子中)。
  • 另外,我投票赞成你的答案,但不幸的是,在我获得 15+ 声望之前投票不会显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
相关资源
最近更新 更多