your children function can return only one child, and one parent can have more children. You should create an array of all children and loop that.

Gerard Samuel wrote:
Im trying to dynamically construct a multidimensional array
to be used with PEAR's HTML_Menu.
http://pear.php.net/manual/en/package.html.html-menu.intro.php.

Basically, Im pulling the data from a DB table, and trying to
attempt to convert the original array to what is needed for HTML_Menu.
If you look at the output (at the bottom of this message),
you'll see that after "Edit Account" it breaks to hell.

Any help in this matter would be very much appreciated.
Thanks

-- Script --
<?php

header('content-type: text/plain');

$m = array (
  0 =>
  array (
    'id' => '0000000000000000',
    'pid' => '0',
    'name' => 'Home',
    'url' => 'index.php',
  ),
  1 =>
  array (
    'id' => 'AB4wFQI2QUewz3P7',
    'pid' => '0000000000000000',
    'name' => 'User',
    'url' => 'modules/user/index.php',
  ),
  2 =>
  array (
    'id' => 'AB4wFQI2QU2iY4SP',
    'pid' => 'AB4wFQI2QUewz3P7',
    'name' => 'Admin',
    'url' => 'modules/user/admin/index.php',
  ),
  3 =>
  array (
    'id' => 'AB4wFQI2QU2ihsz2',
    'pid' => 'AB4wFQI2QU2iY4SP',
    'name' => 'delete',
    'url' => 'modules/user/admin/list_to_delete.php',
  ),
  4 =>
  array (
    'id' => 'AB4wFQI2QUp0uEk7',
    'pid' => 'AB4wFQI2QUewz3P7',
    'name' => 'Edit Account',
    'url' => 'modules/user/edit_account.php',
  ),
  5 =>
  array (
    'id' => 'AB4wFQI2QUp2huwX',
    'pid' => 'AB4wFQI2QUewz3P7',
    'name' => 'Login',
    'url' => 'modules/user/login.php',
  ),
  6 =>
  array (
    'id' => 'AB4wFQI2QUpzujZ8',
    'pid' => 'AB4wFQI2QUewz3P7',
    'name' => 'Register',
    'url' => 'modules/user/register.php',
  ),
);

//var_export($m);die;
var_export(foo());

function foo($key = 0)
{
    $data = array();

    for($x = $key; $x < count($GLOBALS['m']); $x++)
    {
        $id = $GLOBALS['m'][$x]['id'];
        $name = $GLOBALS['m'][$x]['name'];

        $data[$id]['name'] = $name;

        if (FALSE !== ($child = children( $id )))
        {
            $data[$id]['sub'] = foo( $child );
            $x = $child;
        }
        else
        {
            break;
        }
    }

    return $data;
}

function children($id)
{
    foreach($GLOBALS['m'] as $key => $value)
    {
        if ($id === $value['pid'])
        {
            return $key;
        }
    }

    return FALSE;
}

?>
-- End Script --

-- Start Output --
array (
  '0000000000000000' =>
  array (
    'name' => 'Home',
    'sub' =>
    array (
      'AB4wFQI2QUewz3P7' =>
      array (
        'name' => 'User',
        'sub' =>
        array (
          'AB4wFQI2QU2iY4SP' =>
          array (
            'name' => 'Admin',
            'sub' =>
            array (
              'AB4wFQI2QU2ihsz2' =>
              array (
                'name' => 'delete',
              ),
            ),
          ),
          'AB4wFQI2QUp0uEk7' =>
          array (
            'name' => 'Edit Account',
          ),
        ),
      ),
      'AB4wFQI2QU2ihsz2' =>
      array (
        'name' => 'delete',
      ),
    ),
  ),
  'AB4wFQI2QU2iY4SP' =>
  array (
    'name' => 'Admin',
    'sub' =>
    array (
      'AB4wFQI2QU2ihsz2' =>
      array (
        'name' => 'delete',
      ),
    ),
  ),
  'AB4wFQI2QUp0uEk7' =>
  array (
    'name' => 'Edit Account',
  ),
)
-- End Output --


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to