tags:

views:

90

answers:

2

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
A: 
$conv = array();
foreach($nav['sidebar'] as $index => $data)
    foreach($data as $name => $entries)
        foreach($entries as $entry)
            $conv[$name][] = $entry;
$nav['sidebar'] = $conv;
chaos
chaos, thank you for this. Could you have a look up top? I just posted something similer to what I need. Can you tell me if your solution will produce this?
JimS
The results aren't exactly like that; they're more sensibly structured. Any particular reason you wouldn't just use `print_r()` to see for yourself what results my code gives?
chaos
Because he doesn't actually know PHP.
Azeem.Butt
hehe.. That's true... :) I'm trying it now though.
JimS
Ok, I get an empty array. Where are your curly brackets at?
JimS
@NSD: Apparently. @JimS: This code needs no curly brackets. It does, however, need you to change `$nav` to the name of the variable you keep this data structure in, which you have not told us.
chaos
chaos, I did change the name. Believe it or not, I'm not a total noob as nsd would have you believe. Anyhow. I have been up all night and the mind is no longer functioning properly. Aparently, as I just noticed, at some point I must have changed my array structure because it looks nothing like what I posted. I've been trying to hammer this thing any which way and kind of lost track of what I've been doing. I have this array that I am having a hard time building properly. Would you give me a hand so I know its correct?
JimS
A: 

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://
        )

)
xunix
Hi xunix. thanks. I will try this and come back
JimS
xunix, this whole thing is messed up. I must have changed my array at some point because now it doesn't even look like what I posted above. Would you mind giving me a hand building the array, please?
JimS
i've edit the code above.
xunix
Thanks xunix. It doesn't work. Please have a look at the original post above. I have posted the correct (what I believe is correct anyhow) array structure.
JimS
how abut the URL? is there URL on the array?
xunix
i've edit my answer based on last array structure.
xunix