dopnpoh056622 2013-04-01 07:48
浏览 56
已采纳

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

a MYSQL Query is getting me this array of objects (shortened a bit):

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
        )
    ...
)

How is it possible to build an unordered Html List out of this array which looks like this:

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

The first two objects are the same, thus in the ul the person + title should only be listed once under parent winner 2009. The winner from 2011 has two winner projects thus these two different projects should be listed under him.

My approach was to have a foreach loop which fills an array

foreach ($results as $object) { 

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

}

and kick out the doubles

$array = array_unique($array);

this way seems to be extremly unhandy and wrong.

thanks for any help,

tony

  • 写回答

1条回答 默认 最新

  • dscqrkvr9562034621 2013-04-01 08:49
    关注

    You can make use of 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

    The print_r output:

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

    The HTML output (not indented):

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

    If you want to go further fancy, you can change the foreach clause into another array_walk or array_reduce or something like that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制