以表格格式排列数组

时间:2016-04-21 09:03:36

标签: php arrays magento

这是我的数组,我想以表格格式设置这个数组,我想设置第一行如下所示:

    $_tierPrices = Array
(
    Array
        (   
            "price_qty" => "4",
            "price" => "143.00",
            "savePercent" => "8"      
        ),

    Array
        (   
            "price_qty" => "12",
            "price" => "133.0000",
            "savePercent" => "15"
        ),

    Array
        (   
            "price_qty" => "20",
            "savePercent" => "18",
            "price" => "128.0000"
        ),

    Array
        (
            "price_qty" => "40",
            "savePercent" => "21",
            "price" => "123.0000"
        )
);

我想以表格格式设置此数组值,如下所示

这是我的表格行:

4   | 12 |  20 | 40 |

我需要这个:

4 - 11 | 12 - 19 | 20 - 39 | 40+ |

请指教。

3 个答案:

答案 0 :(得分:0)

我不知道你在$_tierPrices中拥有什么,所以我给你一些其他数据的例子。

$arr = array('4','12','20','40');  //suppose you have array like this
$end = end($arr);  //We need to find last element of the array to show like 40+

for($i=0;$i<count($arr);$i++){
  if($arr[$i] != $end){
    echo $arr[$i]."-".($arr[$i+1]-1)." | ";    
  } else{
    echo $arr[$i]."+";    
  }
}

输出就像

4-11 | 12-19 | 20-39 | 40+

答案 1 :(得分:0)

<?php
echo "<table><tr>";//opening the table
$first_iteration = FALSE;

foreach ($_tierPrices as $value)
{
  if (!$first_iteration)//code that only runs on the first iteration
  {
    $first_iteration = TRUE;
    $buffer_value = $value;
  }
  else {//code that runs after the first iteration
    $buff = ((integer)$value)-1;//calculation1 value previous
    $output = $buffer_value."-".$buff;
    echo "<td>".$output."</td>";
    $buffer_value = $value;
  }
}
echo "<td>".$buffer_value."+</td>";
echo "</tr></table>";//closing the table
?>

让我知道这是否适合你:)

答案 2 :(得分:0)

试试这个:

  

Tier Price Array

<?php
    $_tierPrices = array (
        array
        (
            "price_qty" => "4",
            "price" => "143.00",
            "savePercent" => "8"      
        ),

        array
        (   
            "price_qty" => "12",
            "price" => "133.0000",
            "savePercent" => "15"
        ),

        array
        (   
            "price_qty" => "20",
            "savePercent" => "18",
            "price" => "128.0000"
        ),

        array
        (
            "price_qty" => "40",
            "savePercent" => "21",
            "price" => "123.0000"
        )
    );
?>
  

等级价格表

<table>
    <tr><td>Header</td></tr>
    <?php $_qtyRow = '<tr>'; ?>
    <?php $_priceRow = '<tr>'; ?>
    <?php $_saveRow = '<tr>'; ?>
    <?php
        $size = count($_tierPrices);
        foreach($_tierPrices as $index => $_tierPrice) {
            if($index < $size-1) {
                $_qty = $_tierPrice['price_qty'] . ' - ' . ($_tierPrices[$index+1]['price_qty'] - 1);
            } else {
                $_qty = $_tierPrice['price_qty'] . '+';             
            }
            $_qtyRow .= '<td>'.$_qty.'</td>';
            $_priceRow .= '<td>'.$_tierPrice['price'].'</td>';
            $_saveRow .= '<td>'.$_tierPrice['savePercent'].'</td>';
        }

        $_qtyRow .= '</tr>';
        $_priceRow .= '</tr>';
        $_saveRow .= '</tr>';

        echo $_qtyRow;
        echo $_priceRow;
        echo $_saveRow;
    ?>
    <tr><td>Footer</td></tr>
</table>

我希望这会有所帮助。