将多维数组转换为ul li

时间:2014-10-20 05:11:39

标签: php

我有关注数组

$array = array(
        array("title" => "soup", 
            array(
                "name" => "miso soup",
                "description" => "popular japanese soup",
                "price" => "7.00","8.00",
            ),
            array(
                "name" => "seafood soup",
                "description" => "very delicious soup",
                "price" => "10.00","18.00",
                ),
        ),
        array("title" => "rice", 
            array(
                "name" => "miso soup",
                "description" => "popular japanese soup",
                "price" => "7.00","8.00",
            ),
            array(
                "name" => "seafood soup",
                "description" => "very delicious soup",
                "price" => "10.00","18.00",
            ),
        )
    );

我的最终目标是创建像

这样的列表
    <h1>soup</h1>
<ul>
    <li>miso soup <p>popular japanese soup</p><span>7.00<span><span>8.00</span></li>
    <li>seafood soup <p>very delicious soup</p><span>10.00</span><span>18.00</span></li>
</ul>
<h1>rice</h1>
<ul>
    <li>miso soup <p>popular japanese soup</p><span>7.00<span><span>8.00</span></li>
    <li>seafood soup <p>very delicious soup</p><span>10.00</span><span>18.00</span></li>
</ul>

我尝试在foreach中进行循环,结果是重复的li。

任何帮助将不胜感激!提前谢谢!

3 个答案:

答案 0 :(得分:1)

您应该更改数组的设计,因为在顶部数组中会有条目&#34; title&#34;,&#34; 0&#34;和&#34; 1&#34;这使它变得更加困难。此外,语法不会使您想要的价格。像这样:

$array = array(
    array(
        "title" => "soup",
        "data" => array(
            array(
                "name" => "miso soup",
                "description" => "popular japanese soup",
                "price" => array("7.00","8.00"),
            ),
            array(
                "name" => "seafood soup",
                "description" => "very delicious soup",
                "price" => array("10.00","18.00"),
            )
        ),
    ),
    array(
        "title" => "rice", 
        "data" => array(
            array(
                "name" => "miso soup",
                "description" => "popular japanese soup",
                "price" => array("7.00","8.00"),
            ),
            array(
                "name" => "seafood soup",
                "description" => "very delicious soup",
                "price" => array("10.00","18.00"),
            )
        )
    )
);

最后,您可以像这样创建输出:

foreach ($array as $entry) {
    echo "<h1>".$entry['title']."</h1>";
    echo "<ul>";
    foreach ($entry['data'] as $data) {
        echo "<li>".$data['name']." <p>".$data['description']."</p>";
        foreach ($data['price'] as $price) {
            echo "<span>".$price."</span>";
        }
    }
    echo "</ul>";
}

答案 1 :(得分:0)

我希望这会有所帮助

foreach($array as $row) {
    echo '<h1>'.$row['title'].'</h1>';
    unset($row['title']);
    echo '<ul>';
    foreach($row as $list) {
        echo '<li>'.$list['name'].'<p>'.$list['description'].'</p><span>'.$list['price'].'</span><span>'.$list[0].'</span></li>';
    }
    echo '</ul>';
}

答案 2 :(得分:0)

你也可以试试这个:

foreach($array as $item){
    echo "<h1>".$item['title']."</h1>";
    echo "<ul>";
    for($i=0;$i<count($item)-1;$i++)
    {
        $des=$item[$i];
        echo "<li>".$des['name'];           
        echo "<p>".$des['description']."</p>";
        echo "<p>".$des['price']."</p>";            
        echo "</li>";
    }
    echo "</ul>";   
}