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

There are two products with different quantities and prices

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


    simple product -2 :         qty      price
                                 2        195
                                 5        175
                                 9        170

I want to display the quantity and prices in the following format:

qty : 2          3          5       9         10

     $150       $145       $130      -        $100

     $195         -        $175     $170       -  

Below is the code that displays price and qty

/*
$_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; ?>

Below is the actual output of the code.

qty :     2          3          5       9         10 

         $150       $145       $130    $100

         $195       $175       $170      

How do I add "-" when there is no price value in the array

The main logic error in your code, is that the array elements is not equal to the numbers above.

What you need to do is to create an array with the elements and the [-] in the keys you want to show it.

Try this:

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

If it does not work:

Replace:

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

Comment or edit your question. Tell us what is output of:

echo "<pre>"; print_r($price); echo "</pre>";