How can I create an array when there are duplicate values?

I have data containing links for a navigation bar. It should be structured exactly like an unordered list, with a heading and then with all the relevant links below that heading. I can't seem to build this right. These will be some examples of data from the database.

HEADING     LIST         LINK
Favs        google          http://...
Favs        yahoo           http://...
Favs        stackoverflow   http://...
Site        first link      http://...
Site        second link     http://...

      

This data should then group all the headings into one, and then display the associated links. Is this possible, perhaps there is a better way?

I am planning to use "HEADING" and "LIST" to dynamically create menus <UL>

.


Well it doesn't work as I hoped. Here is the array being built from the database. Notice how sidebar [0] and sidebar [1] repeat the "Favs" value. This will echo the same value on my form, which I don't want. All duplicate names must be grouped together. Is it possible?

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

                )

            [1] => Array
                (
                    [Favs] => Array
                        (
                            [author_sidebar_link] => yahoo.com
                        )

                )

            [2] => Array
                (
                    [Offsite] => Array
                        (
                            [author_sidebar_link_title] => something
                        )

                )

            [3] => Array
                (
                    [Something] => Array
                        (
                            [author_sidebar_link] => something else
                        )
                )
        )
)

      

+2


source to share


3 answers


You can use a multidimensional array like this:



<?php
$menu = array(
     'Favs' => array(
         array('LIST' => 'google', 'LINK' => 'http://...'),
         array('LIST' => 'yahoo', 'LINK' => 'http://...'),
         array('LIST' => 'stackoverflow', 'LINK' => 'http://...')
     ),
     'Site' => array(
         array('LIST' => 'first link', 'LINK' => 'http://...'),
         array('LIST' => 'second link', 'LINK' => 'http://...')
     )
);
?>

      

+3


source


$menu = array('Favs' => array(
                              'Google' => 'http://',
                              'Yahoo' => 'http://'
                        ),
              'Site' => array(
                              'First' => 'http://',
                              'Second' => 'http://'
                        )
             );
foreach($menu as $category => $items){
    echo '<h3>' . $category . '</h3>';
    echo '<ul>';
    foreach($items as $name => $url){
        echo '<li><a href="' . $url . '">' . $name . '</a></li>';
    }
    echo '</ul>';
}

      



+1


source


you can use this code

<?php
$_list = array();
foreach($_data as $k => $v){
 $_list[$v['HEADING']][$v['LIST']] = $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>";
}
?>

      

0


source







All Articles