PHP动态菜单

I'm trying to learn how a dynamic menu works. I found a tutorial, and I'm able to build a dynamic menu based upon array down below.

$menu_items = array(
  (object) array('id'=>'1', 'name'=>'Home', 'parent_menu_id'=>'0'),
  (object) array('id'=>'2', 'name'=>'Products', 'parent_menu_id'=>'0'),
  (object) array('id'=>'3', 'name'=>'About', 'parent_menu_id'=>'0'),
  (object) array('id'=>'4', 'name'=>'Contact', 'parent_menu_id'=>'0'),
  (object) array('id'=>'5', 'name'=>'Apparel', 'parent_menu_id'=>'2'),
  (object) array('id'=>'6', 'name'=>'Shirts', 'parent_menu_id'=>'2'),
  (object) array('id'=>'7', 'name'=>'Pants', 'parent_menu_id'=>'2'),
  (object) array('id'=>'8', 'name'=>'Hats', 'parent_menu_id'=>'5'),
  (object) array('id'=>'9', 'name'=>'Gloves', 'parent_menu_id'=>'5'),
  (object) array('id'=>'10', 'name'=>'Ballcaps', 'parent_menu_id'=>'8'),
  (object) array('id'=>'11', 'name'=>'Beanies', 'parent_menu_id'=>'8'),
  (object) array('id'=>'12', 'name'=>'Wool', 'parent_menu_id'=>'11'),
  (object) array('id'=>'13', 'name'=>'Polyester', 'parent_menu_id'=>'11'),
  (object) array('id'=>'14', 'name'=>'Jimsider.com', 'parent_menu_id'=>'4'),
 );

Now for my issue. I want to be able to populate this array from MySQL. I tried to accomplish this with a fetchall, which does make me an array, but the array differs too much from the PHP array above. (Checked using var_dump)

I made a MySQL table with the following columns : id, name, parent_menu_id. I inserted the info as above.

How would I get my db info in the same array-type as the PHP array above?

The way the array looks from PHP:

array(14) { [0]=> object(stdClass)#15 (3) { ["id"]=> string(1) "1" ["name"]=> string(4) "Home" ["parent_menu_id"]=> string(1) "0" } [1]=> object(stdClass)#14 (3) { ["id"]=> string(1) "2" ["name"]=> string(8) "Products" ["parent_menu_id"]=> string(1) "0" } [2]=> object(stdClass)#13 (3) { ["id"]=> string(1) "3" ["name"]=> string(5) "About" ["parent_menu_id"]=> string(1) "0" } [3]=> object(stdClass)#12 (3) { ["id"]=> string(1) "4" ["name"]=> string(7) "Contact" ["parent_menu_id"]=> string(1) "0" } [4]=> object(stdClass)#11 (3) { ["id"]=> string(1) "5" ["name"]=> string(7) "Apparel" ["parent_menu_id"]=> string(1) "2" } [5]=> object(stdClass)#10 (3) { ["id"]=> string(1) "6" ["name"]=> string(6) "Shirts" ["parent_menu_id"]=> string(1) "2" } [6]=> object(stdClass)#9 (3) { ["id"]=> string(1) "7" ["name"]=> string(5) "Pants" ["parent_menu_id"]=> string(1) "2" } [7]=> object(stdClass)#8 (3) { ["id"]=> string(1) "8" ["name"]=> string(4) "Hats" ["parent_menu_id"]=> string(1) "5" } [8]=> object(stdClass)#7 (3) { ["id"]=> string(1) "9" ["name"]=> string(6) "Gloves" ["parent_menu_id"]=> string(1) "5" } [9]=> object(stdClass)#6 (3) { ["id"]=> string(2) "10" ["name"]=> string(8) "Ballcaps" ["parent_menu_id"]=> string(1) "8" } [10]=> object(stdClass)#5 (3) { ["id"]=> string(2) "11" ["name"]=> string(7) "Beanies" ["parent_menu_id"]=> string(1) "8" } [11]=> object(stdClass)#4 (3) { ["id"]=> string(2) "12" ["name"]=> string(4) "Wool" ["parent_menu_id"]=> string(2) "11" } [12]=> object(stdClass)#3 (3) { ["id"]=> string(2) "13" ["name"]=> string(9) "Polyester" ["parent_menu_id"]=> string(2) "11" } [13]=> object(stdClass)#2 (3) { ["id"]=> string(2) "14" ["name"]=> string(12) "Jimsider.com" 

I tried to do a foreach and populate it .but then only the last entry in my db is in the array.

try {
$conn = new PDO($dblink, $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $data = $conn->query('SELECT * FROM menu
');

   foreach($data as $row) { 

          $menu_items = array(
            (object) array('id'=>''.sprintf($row[id]).'', 'name'=>''.sprintf($row[name]).'', 'parent_menu_id'=>''.sprintf($row[parent_menu_id]).''),

}
} catch(PDOException $e) {
 echo 'ERROR: ' . $e->getMessage();
}                        

The way the array looks with the foreach attempt :

array(1) { [0]=> object(stdClass)#5 (3) { ["id"]=> string(1) "3" ["name"]=> string(8) "Over ons" ["parent_menu_id"]=> string(1) "0" } }

edit : when using brackets to fill the array : the foreach seems to be stuck in a loop?

try {
$conn = new PDO($dblink, $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $data = $conn->query('SELECT id,name,parent_menu_id FROM menu
');

   foreach($data as $row) { 

      $menu_items[] = array(
        (object) array('id'=>''.$row[id].'', 'name'=>''.$row[name].'',       'parent_menu_id'=>''.$row[parent_menu_id].''),



 );
      }
 } catch(PDOException $e) {
 echo 'ERROR: ' . $e->getMessage();
 }        

Okay, every row here is assigning to $menu_items:

foreach($data as $row) { 
      $menu_items = array(
        (object) array('id'=>''.sprintf($row[id]).'', 'name'=>''.sprintf($row[name]).'', 'parent_menu_id'=>''.sprintf($row[parent_menu_id]).''),

}

And it should be added to $menu_items:

foreach($data as $row) { 
     $menu_items[] = (object) array(
         'id'=>''.sprintf($row[id]).'', 
         'name'=>''.sprintf($row[name]).'',
         'parent_menu_id'=>''.sprintf($row[parent_menu_id]).''
     );
}