PHP使用Object数组的值填充无序List

时间:2013-04-01 07:48:24

标签: php arrays object html-lists unique

一个MYSQL查询给我这个对象数组(缩短了一点):

Array
 (
    [0] => stdClass Object
        (
            [parentTitle] => Winner 2009
            [catTitle] => Max Muster
            [title] => drm202
        )

    [1] => stdClass Object
        (
            [parentTitle] => Winner 2009
            [catTitle] => Max Muster
            [title] => drm202
        )

    [2] => stdClass Object
        (
            [parentTitle] => Winner 2011
            [catTitle] => Josh Frank
            [title] => A bird in the cage
        )

    [3] => stdClass Object
        (
            [parentTitle] => Winner 2011
            [catTitle] => Josh Frank
            [title] => cue & repeat
        )
    ...
)

如何从这个数组中构建一个无序的Html List,如下所示:

<ul id="content">

    <li>Winner 2009
       <ul>
          <li>Max Muster
             <ul>
                  <li>drm202</li> 
             </ul>
          </li>
       </ul>
   </li>

    <li>Winner 2011
       <ul>
          <li>Josh Frank
             <ul>
                  <li>A bird in the cage</li> 
                  <li>cue & repeat</li> 
             </ul>
          </li>
       </ul>
   </li>

</ul>

前两个对象是相同的,因此在ul中,人+标题只应在父母获胜者2009下列出一次。2011年的获胜者有两个获胜项目,因此这两个不同的项目应列在他的下面。

我的方法是使用一个填充数组的foreach循环

foreach ($results as $object) { 

    // fill array
    $array[] = "</ul><ul><li><h2> " . $object->parentTitle . "</h2>\n</li>";
    $array[] = "<li><h5>" . $object->catTitle . "</h5></li>\n";
    $array[] = "<li> - " . $object->title . "</li>\n";

}

并踢出双打

$array = array_unique($array);

这种方式似乎极其不利和错误。

感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

您可以使用array_walk

$source=json_decode('[{"parentTitle":"Winner 2009","catTitle":"Mas Muster","title":"drm202"},{"parentTitle":"Winner 2009","catTitle":"Mas Muster","title":"drm202"},{"parentTitle":"Winner 2011","catTitle":"Josh Frank","title":"A bird in the cage"},{"parentTitle":"Winner 2011","catTitle":"Josh Frank","title":"cue & repeat"}]');
$result=array();
array_walk($source,function($v,$i)use(&$result){
    if(!isset($result[$v->parentTitle])) $result[$v->parentTitle]=array();
    if(!isset($result[$v->parentTitle][$v->catTitle])) $result[$v->parentTitle][$v->catTitle]=array();
    if(!in_array($v->title,$result[$v->parentTitle][$v->catTitle])) $result[$v->parentTitle][$v->catTitle][]=$v->title;
});
print_r($result); // just to debug
echo "<ul>";
foreach($result as $key=>$sub){
    echo "<li>".htmlentities($key,ENT_COMPAT,"UTF-8");
    echo "<ul>";
    foreach($sub as $k=>$s){
        echo "<li>".htmlentities($k,ENT_COMPAT,"UTF-8");
        echo "<ul>";
        foreach($s as $i=>$v){
            echo "<li>".htmlentities($v,ENT_COMPAT,"UTF-8")."</li>";
        }
        echo "</ul></li>";
    }
    echo "</ul></li>";
}
echo "</ul>";

Live demo

print_r输出:

Array
(
    [Winner 2009] => Array
        (
            [Mas Muster] => Array
                (
                    [0] => drm202
                )

        )

    [Winner 2011] => Array
        (
            [Josh Frank] => Array
                (
                    [0] => A bird in the cage
                    [1] => cue & repeat
                )

        )

)

HTML输出(不缩进):

<ul><li>Winner 2009
<ul><li>Mas Muster
<ul><li>drm202</li>
</ul>
</li></ul>
</li><li>Winner 2011
<ul><li>Josh Frank
<ul><li>A bird in the cage</li>
<li>cue &amp; repeat</li>
</ul>
</li></ul>
</li></ul>

如果您想进一步了解,可以将foreach条款更改为其他array_walkarray_reduce或类似内容。