Associated array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NinjaBot
Forum Newbie
Posts: 7
Joined: Sat Apr 29, 2006 11:54 am

Associated array problem

Post by NinjaBot »

I've got a function that lists pages from several directories. However, I am having trouble ordering them (at present, they're ordered as groups from their respective parents, rather than collectively as I want):

Code: Select all

<div class="members">
<ul>
<?php
$members = array();
function snippet_fullmenu($parent){
    global $members;
    $out = '';
    $childs = $parent->children(null, array(), true);
    $exclude = array("");
    if (count($childs) > 0){
    foreach ($childs as $child)
        /* Don't display specific pages */
        if(!in_array($child->breadcrumb(),$exclude)){
            $depth = substr_count($child->link(), '/');
            if($depth == 7){    
                $split_name = explode(" ", $child->title());
                $surname = isset($split_name[2]) ? $split_name[2] : $split_name[1]; 
                $entry = '<li>'.$child->link($child->breadcrumb.', '.$child->content('practice')).', '.$surname.'</li>'."\r";
                $out .= $entry;
                $members[$surname] = $entry;
            }
            $out .= snippet_fullmenu($child);
        }
    }
    return $out;
}
?>
<?php
 
echo snippet_fullmenu($this->find('/members/'));
 
    //ksort($members);
    //echo $members['Louw'];
    //foreach ($members as $s => $value){
    //print $count++;
        //print($value . "<br>");
    //}
 
?>
</ul>
</div>
If you look at the commented array that I try to print at the bottom, you can see I have tried to return an ordered list by adding my results to an associated members array as an alternative, however it appears to duplicate the results.

Can anyone help!?
NinjaBot
Forum Newbie
Posts: 7
Joined: Sat Apr 29, 2006 11:54 am

Re: Associated array problem

Post by NinjaBot »

I got it ... my members array was set as global within the function, but not outside the function. Setting the members array to global outside of the function allowed the array to be built correctly.
Post Reply