/***************************by garcon1986********************************/ 一个三维数组的显示,保存以备以后使用。 <!DOCTYPE html> <html> <head> </head> <body> <?php error_reporting(E_ALL ^ E_NOTICE); $conn = mysql_connect("localhost","charles","charles"); mysql_select_db("crm1204", $conn); //为了更好的显示for better display function dump($vars, $label = null, $return = false) { if (ini_get('html_errors')) { $content = "<pre>/n"; if ($label !== null && $label !== '') { $content .= "<b>{$label} :</b>/n"; } $content .= htmlspecialchars(print_r($vars, true)); $content .= "/n</pre>/n"; } else { $content = "/n"; if ($label !== null && $label !== '') { $content .= $label . " :/n"; } $content .= print_r($vars, true) . "/n"; } if ($return) { return $content; } echo $content; return null; } /* 不好的例子 $query = "select * from entreprise"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)){ $query2 = "select * from site where entreprise_id = $row[entreprise_id]"; $result2 = mysql_query($query2); $a2 = array(); while($row2 = mysql_fetch_assoc($result2)){ $query3 = "select * from salarie where site_id = $row2[site_id]"; $result3 = mysql_query($query3); while($row3 = mysql_fetch_assoc($result3)){ $a3[] = array("text"=>$row3[nom]); } if($a3[site_id] == $a2[site_id]){ $a2[] = array("text"=>$row2[nom],'children'=>$a3); } } if($a2[entreprise_id] == $a1[entreprise_id]){ $a1[] = array("text"=>$row[id]." ".$row[nom],'children'=>$a2); } } //print_r($a1); //echo json_encode($a1); dump($a1); */ $entreprise_q = mysql_query("select * from entreprise"); $site_q = mysql_query("select * from site"); $salarie_q = mysql_query("select * from salarie"); $result = array(); $entreprise = array(); $site = array(); $salarie = array(); while($row = mysql_fetch_assoc($salarie_q)){ if(!is_array($salarie[$row['site_id']])){ $salarie[$row['site_id']] = array(); } $salarie[$row['site_id']][$row['salarie_id']] = $row['nom']; } while($row = mysql_fetch_assoc($site_q)){ if(!is_array($site[$row['entreprise_id']])){ $site[$row['entreprise_id']] = array(); } $site[$row['entreprise_id']][$row['site_id']] = array( 'name' => $row['nom'], 'salarie' => $salarie[$row['site_id']] ); } while($row = mysql_fetch_assoc($entreprise_q)){ if(!is_array($entreprise[$row['entreprise_id']])){ $entreprise[$row['entreprise_id']] = array(); } $entreprise[$row['entreprise_id']] = array( 'name' => $row['nom'], 'sites' => $site[$row['entreprise_id']] ); } //echo json_encode($entreprise); dump($entreprise); ?> </body> </html>