-4

I am trying to display the array data in a specific way in a table, so I am needing to format my array like this:

               Leafy Life - Aspley - All Green
Callistemon1 -     33     -        - 
Callistemon2 -            -        - 3.59
Acronychia   -            -  22.5  - 

I have tried many times, but my knowledge is limited and would really appreciate it if anyone can point me in the right direction, or even if you have time to adjust my code that would be very much appreciated. <?php

$options = array(
array("plant" => "Callistemon Kings Park Special 300 mm","nursery" => "A Leafy Life","price" => "33"),
array("plant" => "Callistemon Kings Park Special 300 mm","nursery" => "Alpine Nurseries Alstonville","price" => "23"),
array("plant" => "Callistemon Kings Park Special 140 mm","nursery" => "All Green Nursery","price" => "3.59"),
array("plant" => "Acronychia imperforata 300 mm","nursery" => "Aspley Nursery","price" => "22.5"),
array("plant" => "Acronychia imperforata 300 mm","nursery" => "All Green Nursery","price" => "23"),
array("plant" => "Metrosideros collina Little Dugald  140 mm","nursery" => "Accent Plants","price" => "5.25"),
);

$newOptions = array();
foreach ($options as $option) {
 $plant = $option['plant'];
 $nursery = $option['nursery'];
 $price = $option['price'];

 $newOptions[$plant][$nursery] = $price;
}

print_r($newOptions);

echo "<table cellpadding='0' cellspacing='0' border='2'>";
echo "<thead>";
echo "<th></th>";
foreach($newOptions as $key => $row) 
{ 
    foreach($row as $k => $v) 
    {
        print_r($k);
        echo "<th>";
        echo $k;
        echo "</th>";
    }       
}
echo "</thead>";

echo "<tbody>";
$i=0;
foreach($newOptions as $keys => $rows) 
{ 
    echo "<tr>";
    echo "<td>";
    echo $keys;
    echo "</td>";

    foreach($rows as $v) 
    {
        
            echo "<td>";
            echo "&nbsp;". $i;
            echo "</td>";
        
        $i++;
        echo "<td>";
        echo $v;
        echo "</td>";
        
    }
    
    echo "</tr>";
}

echo "</tbody>";
echo "</table>";


?>
4

1 に答える 1

1

このように配列の構造を変更できます-

$Headers = array();
$headers[1] = "Leafy Life";
$headers[2] = "Aspley";
$headers[3] = "All Green";

$firstCloumnEntries = array();
$firstCloumnEntries[1] = "Callistemon1";
$firstCloumnEntries[2] = "Callistemon2";
$firstCloumnEntries[3] = "Acronychia";

$values = array();
$values[1] = array(); // this will have all values for first row;
$values[1][1] = "33";

$values[2] = array(); // this will have all values for second row;
$values[2][3] = "3.59";

$values[1] = array(); // this will have all values for third row;
$values[3][2] = "22.5";

表示中 - 最初にすべての列の「ヘッダー」配列からすべてを追加します。

次に、各行について、「firstCloumnEntries」配列を使用して、ループのインデックスを使用して最初の値を取得します。対応する行の値を出力する内部ループを記述します。その値を出力する前に、を使用してisset()ください。

インデックス "0" は、0 列 0 行に値がないため、意図的に削除されています。ヘッダー配列または firstCloumnEntries 配列のインデックスは、その中の要素の ID を理解するのに役立ちます。たとえば、1 は「Leafy Life」の ID、2 は「Aspley」などです。これを使用して、さらにヘッダーを追加できます。新しい ID と、より多くの ID を持つより多くの firstCloumnEntries。これは、動的コンテンツの追加に役立ち、ループを簡単にします。

于 2013-05-18T11:19:55.817 に答える