PHP添加符号,如果没有价格

PHP添加符号,如果没有价格,php,magento-1.7,Php,Magento 1.7,有两种不同数量和价格的产品 simple product -1 : qty price 2 150 3 145 5 130 10 100

有两种不同数量和价格的产品

    simple product -1 :         qty      price
                                 2        150
                                 3        145
                                 5        130
                                 10      100


    simple product -2 :         qty      price
                                 2        195
                                 5        175
                                 9        170
我想以以下格式显示数量和价格:

qty : 2          3          5       9         10

     $150       $145       $130      -        $100

     $195         -        $175     $170       -  
下面是显示价格和数量的代码

/*
$_tResult = [2, 3, 5, 9, 2, 5, 10]
*/


     /*
$_tierPrice value


    array(10) {
      ["price_id"] => string(2) "18"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "150.0000"
      ["price_qty"] => float(2)
      ["website_price"] => string(8) "150.0000"
      ["formated_price"] => string(34) "150.00"
      ["savePercent"] => float(4)
      ["formated_price_incl_tax"] => string(34) "150.00"
    }

    array(10) {
      ["price_id"] => string(2) "65"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "120.0000"
      ["price_qty"] => float(3)
      ["website_price"] => string(8) "120.0000"
      ["formated_price"] => string(34) "120.00"
      ["savePercent"] => float(23)
      ["formated_price_incl_tax"] => string(34) "120.00"
    }

    array(10) {
      ["price_id"] => string(2) "61"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "145.0000"
      ["price_qty"] => float(5)
      ["website_price"] => string(8) "145.0000"
      ["formated_price"] => int(5) "145.00"
      ["savePercent"] => float(7)
      ["formated_price_incl_tax"] => string(34) "145.00"
    }

    array(10) {
      ["price_id"] => string(2) "62"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "130.0000"
      ["price_qty"] => float(9)
      ["website_price"] => string(8) "130.0000"
      ["formated_price"] => int(5) "130.00"
      ["savePercent"] => float(17)
      ["formated_price_incl_tax"] => string(34) "130.00"
    }

    array(10) {
      ["price_id"] => string(2) "47"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "190.0000"
      ["price_qty"] => float(2)
      ["website_price"] => string(8) "190.0000"
      ["formated_price"] => int(5) "190.00"
      ["savePercent"] => float(5)
      ["formated_price_incl_tax"] => string(34) "190.00"
    }

    array(10) {
      ["price_id"] => string(2) "63"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "175.0000"
      ["price_qty"] => float(5)
      ["website_price"] => string(8) "175.0000"
      ["formated_price"] => int(5) "175.00"
      ["savePercent"] => float(13)
      ["formated_price_incl_tax"] => string(34) "175.00"
    }

    array(10) {
      ["price_id"] => string(2) "64"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "195.0000"
      ["price_qty"] => float(9)
      ["website_price"] => string(8) "195.0000"
      ["formated_price"] => int(5) "195.00"
      ["savePercent"] => float(3)
      ["formated_price_incl_tax"] => string(34) "195.00"
    }

    array(10) {
      ["price_id"] => string(2) "44"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "170.0000"
      ["price_qty"] => float(10)
      ["website_price"] => string(8) "170.0000"
      ["formated_price"] => int(5) "170.00"
      ["savePercent"] => float(15)
      ["formated_price_incl_tax"] => string(34) "170.00"
    }
    */




 <?php $_item->setData('tier_price',null); ?>
          <?php $_tierPrices = $this->getTierPrices($_item); ?>
          <?php //print_r($_tResult); ?>
          <?php foreach ($_tierPrices as $price): ?>
                  <td>
                        <?php if(in_array($price['price_qty'],$_tResult)) :?>
                               <?php echo $price['formated_price']; ?>
                        <?php else: ?>
                               <?php echo "-"; ?>
                        <?php endif; ?>
                   </td>    
           <?php endforeach; ?>

当数组中没有价格值时,如何添加“-”代码中的主要逻辑错误是数组元素不等于上面的数字

您需要做的是创建一个数组,其中包含要显示的元素和键中的
[-]

试试这个:

<?php $_item->setData('tier_price',null); ?>
          <?php $_tierPrices = $this->getTierPrices($_item); ?>
          <?php //print_r($_tResult); ?>
          <?php foreach ($_tierPrices as $price): ?>
            <td>
               <?= (!empty($price['price'])) ? $price['formated_price']: "-"; ?>
            </td>    
           <?php endforeach; ?>

如果不起作用:

替换:

<?= (!empty($price['price_qty'])) ? $price['formated_price']: "-"; ?>
with
<?= (!empty($price['formated_price'])) ? $price['formated_price']: "-"; ?>

具有
评论或编辑您的问题。告诉我们什么是输出:

echo "<pre>"; print_r($price); echo "</pre>";
echo”“;打印(价格);回声“;

$\u tierplices的值是多少?数组是什么样子的?$\u tierplices只是对代码的一个注释。打开和关闭PHP是一项繁重的操作,您不应该在循环中这样做。我建议您首先使用VTemplate这样的模板引擎,当您熟悉这个概念时,您可以开始使用smarty这样的更强大的引擎,因为您有额外的
。。。还有
else:
是一个打字错误吗?使用isset——在上面的代码中不起作用。您的代码输出看起来也和我上面提到的一样。谢谢。你们看了完整的答案了吗?试过不同的条件吗?
echo”的输出是什么;打印(价格);回声“我得到的五个产品数组输出如下所示:数组([price\u id]=>18[website\u id]=>0[all\u group]=>1[cust\u group]=>32000[price]=>150.0000[price\u qty]=>2[website\u price]=>150.0000[formatted\u price]=>150.00[savePercent]=>4[formatted\u price\u inclu tax]=>150.00美元)我刚才看到。。。您的阵列都没有空白价格。。。请在将数组放入foreach之前重新构造数组
echo "<pre>"; print_r($price); echo "</pre>";