2016-04-21 4 views
-1

Это мой массив, и я хочу, чтобы установить этот массив в виде таблицы, и я хочу, чтобы установить первую строку, как показано ниже:Упорядочить массив в виде таблицы

$_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+ | 

Просьба сообщить.

+0

Покажите нам, что вы попробовали – Epodax

+0

Что такое 'var_dump ($ _ tierPrices);'? – Dray

+0

@Dray - Пожалуйста, найдите результат массива в моем вопросе –

ответ

0

Попробуйте это:

Tier Цена Массив

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

Я надеюсь, что это поможет.

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

дайте мне знать, если это работает для вас :)

Смежные вопросы