0

一个 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 列表,如下所示:

<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 中,person + title 应该只在 parent 获胜者 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);

这种方式似乎非常不方便和错误。

谢谢你的帮助,

托尼

4

1 回答 1

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

现场演示

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类似的内容。

于 2013-04-01T08:49:58.373 回答