PHP多维数组
/***************************bygarcon1986********************************/一个三维数组的显示,保存以备以后使用。
<!DOCTYPE html><html><head></head><body><?phperror_reporting(E_ALL ^ E_NOTICE);$conn = mysql_connect("localhost","charles","charles");mysql_select_db("crm1204", $conn);//为了更好的显示for better displayfunction 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";$result2 = mysql_query($query2);$a2 = array();while($row2 = mysql_fetch_assoc($result2)){$query3 = "select * from salarie where site_id = $row2";$result3 = mysql_query($query3);while($row3 = mysql_fetch_assoc($result3)){$a3[] = array("text"=>$row3);}if($a3 == $a2){$a2[] = array("text"=>$row2,'children'=>$a3); }}if($a2 == $a1){$a1[] = array("text"=>$row." ".$row,'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>
页:
[1]