我试图在表格中以特定方式显示数组数据,所以我需要像这样格式化我的数组:
Leafy Life - Aspley - All Green
Callistemon1 - 33 - -
Callistemon2 - - - 3.59
Acronychia - - 22.5 -
我已经尝试了很多次,但是我的知识有限,如果有人能指出我正确的方向,或者即使你有时间调整我的代码,我将不胜感激。<?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 " ". $i;
echo "</td>";
$i++;
echo "<td>";
echo $v;
echo "</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>