在嵌套结构中显示php数组

时间:2011-12-10 06:09:23

标签: php arrays

array(
[country] => India, [state] => Kerala, [city] => Kottayam)
            array([country] => India, [state] => Bihar, [city] => Vijayavada)
            array([country] => India, [state] => Kerala, [city] => Cochin)
            array([country] => Pakistan, [state] => Sate1, [city] => City1)
            array([country] => Pakistan, [state] => Sate1, [city] => City2)

尝试了array_unique,但由于索引相同,它失败只返回第一个数组

而且我想将上面的数组打印为

<div class="location-content">
    <h3>India</h3>
        <h4>Kerala</h4>
        <ul class="countries">
            <li><a href="#"> Kottayam</a> </li>
            <li><a href="#">Cochin</a> </li>
        </ul>
    <h4>Bihar</h4>
    <ul class="countries">
            <li><a href="#"> Vijayavada</a> </li>
    </ul>
    <h3>Pakistan</h3>
        <h4>Sate1</h4>
        <ul class="countries">
            <li><a href="#"> City1</a> </li>
        </ul>
    <h4>Sate2</h4>
    <ul class="countries">
            <li><a href="#"> City2</a> </li>
    </ul>
<div>

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

function arrayUnique($myArray)
{
    if(!is_array($myArray))
           return $myArray;

    foreach ($myArray as &$myvalue){
        $myvalue=serialize($myvalue);
    }

    $myArray=array_unique($myArray);

    foreach ($myArray as &$myvalue){
        $myvalue=unserialize($myvalue);
    }

    return $myArray;

} 
    echo "<pre>";
    print_r(arrayUnique($arr));