How to loop while array_unique in pdo

anyone can solve my problem
i have table like this
+tbl_menu
-menu_id
-menu_name (news | tips | reviews)

+tbl_sub_menu
-sub_id
-menu_id
-sub_name (phone, tablet, laptop…)

i mean if i code in html it look like this
<ul>
<li>News</li>
<li>
<ul>
<li>phone</li>
<li>teble</li>
<li>labtop</li>
</ul>
</li>
<li>Tips</li>
<li>
<ul>
<li>phone</li>
<li>teble</li>
<li>labtop</li>
</ul>
</li>
</ul>


$field = "`tbl_menu`.`menu_name` , `tbl_sub_menu`.`sub_name`";
$table = "`tbl_menu` ,`tbl_sub_menu` WHERE `tbl_sub_menu`.`menu_id`= `tbl_menu`.`menu_id`";
$statement=selecttest($field,$table);
 while ($row = $statement-&gt;fetch(PDO::FETCH_ASSOC)) {
 	echo $row['menu_name']."|".$row['sub_name']."&lt;br /&gt;";
 }
echo "&lt;hr&gt;";
$row = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
$a = array_unique($row);
echo "&lt;pre&gt;";
print_r($a);
echo "&lt;hr&gt;&lt;br /&gt;";
var_dump($a);

i want to use array_unique() because the database it duplicated result so any way to solve this problem.
Regards,
Heng

I guess the “duplicated result” is the menu name?
You could do it like this


// put the query results in a 2-dimensional array
$menuArray = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
  $menuArray[$row['menu_name']][] =  $row['sub_name'];
} 

// let's dump the array to see what's in it
var_dump($menuArray);

// let's loop through the array and display the results
foreach ($menuArray as $mainmenu => $submenuArray) {
  echo $mainmenu . '<br />';
  foreach ($submenuArray as $submenu) {
    echo $submenu . '<br />';
  }
}

Dear Guido,
You’re awesome honestly i’m not good at array :slight_smile: now it work perfectly that what i wanted.
Thank so much Guido i would give you gold medal you save my life
Regards,
Heng

Dear Guido


$field = "`tbl_menu`.`menu_name` , `tbl_sub_menu`.`sub_name`,`tbl_sub_menu`.`sub_id`";
$table = "`tbl_menu` ,`tbl_sub_menu` WHERE `tbl_sub_menu`.`menu_id`= `tbl_menu`.`menu_id`";	
$statement=selecttest($field,$table);
	$menuArray = array();
	while ($row = $statement-&gt;fetch(PDO::FETCH_ASSOC)) {
		$menuArray[$row['menu_name']=&gt;$row['menu_id']][] =  $row['sub_name'];
	}
	echo '&lt;div id="menu"&gt;';
	echo '&lt;ul id="nav"&gt;';
	foreach ($menuArray as $mainmenu =&gt; $submenuArray) {
        foreach ($mainmenu as $url){
	echo '&lt;li&gt;&lt;a href="'.$url.'"&gt;'.$mainmenu.'&lt;/a&gt;&lt;ul&gt;';
	foreach ($submenuArray as $submenu) {
		echo '&lt;li&gt;&lt;a href="'.$url.'"&gt;'.$submenu . '&lt;/a&gt;&lt;/li&gt;';
         }
	}
	echo '&lt;/ul&gt;&lt;/li&gt;';
}
echo '&lt;/ul&gt;&lt;/div&gt;';

the variable $url is not working because i want to verify each sub menu have an unique menu id easy to calling the right article i know it gonna have 1 more problem when pass 2 parameter into <a href=“article.php?menu_id=$row[‘menu_id’]&sub_id=$row[sub_id]”>$row[‘sub_name’]</a>
any good way better then this??

Regards,
Heng

Sorry, I’ve no idea what you are trying to do. I do see that you made some changes to my code that won’t work (as far as I can tell).

sorry guido now i found the solution thanks for your take time to reply
Regards,
Heng

You might be able to group up your results and purge redundant values at query time.

Try this…


$statement->fetchAll( PDO::FETCH_GROUP| PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC ); 

And yes, that’s legal - using bitwise or to set multiple PDO fetch style flags on the same fetchAll call.