开发者

My array is not giving the correct results

Well, I might as well start a new thread. This isn't working as I had hoped. Here is the array that is being built from the database. Notice how sidebar[0] and sidebar[1] rpeat the value "Favs". This will repeat the same value on my form which I don't want. All of the duplicate names should be grouped together. Is this possible?


Array
(
    [1] => Array
        (
            [date] => Sun, 25 Oct 2009
            [sidebar] => Array
                (
                    [0] => Array
                        (
                            [header] => Favs
                            [link] => google.com
                        )

                    [1] => Array
                        (
                            [header] => Favs
                            [link] => yahoo.com
               开发者_运维百科         )

                    [2] => Array
                        (
                            [header] => Offsite
                            [link] => dfdaf
                        )

                    [3] => Array
                        (
                            [header] => Something
                            [link] => something else
                        )
                )
        )
)

Here is an example of what I need.

The database will more than likely have multiple rows with the same sidebar name like "Favs" or something whatever else. These headings should be grouped into single categories and all of the links grouped.

Favs  google  http://...
Favs  yahoo   http://...
Favs  SO      http://...
bla   bla     http://...
bla   bla1    http://...

Should give:
Favs
  google
  yahoo
  SO
bla
  bla
  bla1


$conv = array();
foreach($nav['sidebar'] as $index => $data)
    foreach($data as $name => $entries)
        foreach($entries as $entry)
            $conv[$name][] = $entry;
$nav['sidebar'] = $conv;


based on your last array structure:

<?php
$_list = array();
foreach($data as $k => $v){ // $data is get from $array['sidebar'];
    $_list[$v['header']][$v['link']] = "";
}
foreach($_list as $k => $v){
    echo "<ul>".$k;
    foreach($v as $kk => $vv){
        echo "<li><a href='".$vv."'>".$kk."</a></li>";
    }
    echo "</ul>";
}
?>

array structure for code above:

Array
(
    [0] => Array
        (
            [header] => Favs
            [link] => google
            [url] => http://
        )

    [1] => Array
        (
            [header] => Favs
            [link] => yahoo
            [url] => http://
        )

    [2] => Array
        (
            [header] => Favs
            [link] => gmail
            [url] => http://
        )

    [3] => Array
        (
            [header] => Site
            [link] => facebook
            [url] => http://
        )

    [4] => Array
        (
            [header] => Site
            [link] => ymail
            [url] => http://
        )

    [5] => Array
        (
            [header] => Site
            [link] => myspace
            [url] => http://
        )

)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜