php格式的输出数组

时间:2013-05-18 11:02:36

标签: php

我需要在表格中以特定的方式显示数组数据,所以我需要像这样格式化我的数组:

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

如果有人能指出我正确的方向,甚至调整我非常感激的代码。     

$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>";


?>

2 个答案:

答案 0 :(得分:0)

我们可以像这样更改数组的结构 -

$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";

现在在显示时 - 首先添加所有列“header”数组中的所有内容。

然后对于每一行 - 使用“firstCloumnEntries”数组使用循环索引获取第一个值。     编写内部循环以打印相应的行值。在打印该值之前,请使用isset()

由于我们在第0列第0行没有任何值,故意删除索引“0”。 标题数组或firstCloumnEntries数组中的索引将帮助我们理解其中元素的ID,例如1是“Leafy Life”的ID,2是“Aspley”等等...使用此我们可以添加更多标题新ID和更多具有更多ID的firstCloumnEntries。 这有助于添加动态内容并简化循环。

答案 1 :(得分:0)

这是我正在使用的实际数组。

array
(
[E Plants Noosa] => Array
    (
        [Delonix regia] => Array
            (
                [price] => 145.00
                [stock_item_id] => 364
                [supplier_id] => 65
            )

        [Festuca glauca] => Array
            (
                [price] => 4.00
                [stock_item_id] => 2325
                [supplier_id] => 65
            )

    )

[Greenstock Nurseries] => Array
    (
        [Delonix regia] => Array
            (
                [price] => 150.00
                [stock_item_id] => 364
                [supplier_id] => 32
            )

    )

[Pallara Trees] => Array
    (
        [Delonix regia] => Array
            (
                [price] => 145.00
                [stock_item_id] => 364
                [supplier_id] => 36
            )

    )

[Plants Direct Queensland] => Array
    (
        [Delonix regia] => Array
            (
                [price] => 145.00
                [stock_item_id] => 364
                [supplier_id] => 35
            )

    )

[Pacific Trees Qld Pty Ltd] => Array
    (
        [Delonix regia] => Array
            (
                [price] => 145.00
                [stock_item_id] => 364
                [supplier_id] => 7
            )

    )

[Mad About Plants] => Array
    (
        [Delonix regia] => Array
            (
                [price] => 135.00
                [stock_item_id] => 364
                [supplier_id] => 168
            )

    )

[Oxford Park Nursery] => Array
    (
        [Poa labillardieri ] => Array
            (
                [price] => 1.50
                [stock_item_id] => 8886
                [supplier_id] => 166
            )

        [Festuca glauca] => Array
            (
                [price] => 2.00
                [stock_item_id] => 2325
                [supplier_id] => 166
            )

    )

[Botanica Nurseries] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 4.95
                [stock_item_id] => 2325
                [supplier_id] => 245
            )

    )

[Andreasens Green] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 4.75
                [stock_item_id] => 2325
                [supplier_id] => 33
            )

    )

[All Green Nursery] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 3.59
                [stock_item_id] => 2325
                [supplier_id] => 251
            )

    )

[Meyer Nurseries] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 3.85
                [stock_item_id] => 2325
                [supplier_id] => 244
            )

    )

[Alderwood Nursery] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 5.00
                [stock_item_id] => 2325
                [supplier_id] => 266
            )

    )

[Exotic Nurseries and Landscaping] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 0.00
                [stock_item_id] => 2325
                [supplier_id] => 270
            )

    )

[Cabbage Tree Nursery] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 5.00
                [stock_item_id] => 2325
                [supplier_id] => 233
            )

    )

[Murphy & Nowland Nursery] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 3.50
                [stock_item_id] => 2325
                [supplier_id] => 200
            )

    )

[Carstens Gardens] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 4.75
                [stock_item_id] => 2325
                [supplier_id] => 263
            )

    )

[Logans Nursery] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 3.80
                [stock_item_id] => 2325
                [supplier_id] => 57
            )

    )

[Kenthurst Nursery] => Array
    (
        [Festuca glauca] => Array
            (
                [price] => 4.70
                [stock_item_id] => 2325
                [supplier_id] => 241
            )

    )

这是我的表

<table cellpadding='0' cellspacing='0' border='2'>
<thead>
<th></th>
<?php
foreach($nurseries as $key => $row) 
{ 
    echo "<th>";
    echo $key;
    echo "</th>";
}       

?>
</thead>
<tbody>
<?php
foreach($nurseries as $keys => $rows) 
{ 
    foreach($rows as $k => $v) 
    {
        echo "<tr>";
        echo "<td>";
        echo $k;
        echo "</td>";

        foreach($v as $key => $s) 
        {
            echo "<td>";
            echo $key;
            echo "</td>";           
        }
    }

    echo "</tr>";
}
?>
</tbody>