Hello,
I want to make a multidimensional array in php. Here is what i have done:
Firstly, i have 3 tables:
entreprise:
enterprise_id name
1 e1
2 e2
site:
site_id entreprise_id name
1 1 e1_site1
2 2 e2_site1
...
salarie:
salarie_id site_id name
1 1 e1_site1_salarie1
2 2 e2_site1_salarie1
...
I have the following PHP code:
$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]);
}
$a2[] = array("text"=>$row2[nom],'children'=>$a3);
}
$a1[] = array("text"=>$row[id]." ".$row[nom],'children'=>$a2);
}
But you can see the ouput is mixed. For example, the 'e1_site1_salarie1_nom' is in 'e1_site2', the 'e1_site1_salarie1_nom' is under 'e2_site2'. It's strange.
Array
(
[0] => Array
(
[text] => e1
[children] => Array
(
[0] => Array
(
[text] => e1_site1
[children] => Array
(
[0] => Array
(
[text] => e1_site1_salarie1_nom
)
)
)
[1] => Array
(
[text] => e1_site2
[children] => Array
(
[0] => Array
(
[text] => e1_site1_salarie1_nom
)
[1] => Array
(
[text] => e1_site2_sa1
)
[2] => Array
(
[text] => e1_site2_sa2
)
)
)
)
)
[1] => Array
(
[text] => e2
[children] => Array
(
[0] => Array
(
[text] => e2_site2
[children] => Array
(
[0] => Array
(
[text] => e1_site1_salarie1_nom
)
[1] => Array
(
[text] => e1_site2_sa1
)
[2] => Array
(
[text] => e1_site2_sa2
)
[3] => Array
(
[text] => e2_site2_salarie2_nom
)
)
)
)
)
)
I think it's the problem of my php code. I think i should use more conditional judgement such as if else etc with the following code.
$a2[] = array("text"=>$row2[nom],'children'=>$a3);
$a1[] = array("text"=>$row[id]." ".$row[nom],'children'=>$a2);
But I don't know how to change it.
Do you have any clue or suggestions?
Thanks in advance.
Edit:
Following Tatu's suggestion, it works. Thanks Tatu. Now i have one more question. I don't want the [1] => Array
in the header of the arrays. How to achieve that?
Array
(
[1] => Array
(
[text] => e1
[children] => Array
(
[1] => Array
(
[text] => e1_site1
[children] => Array
(
[1] => e1_site1_salarie1_nom
)
)
[3] => Array
(
[text] => e1_site2
[children] => Array
(
[3] => e1_site2_sa1
[4] => e1_site2_sa2
)
)
)
)
[2] => Array
(
[text] => e2
[children] => Array
(
[2] => Array
(
[text] => e2_site2
[children] => Array
(
[2] => e2_site2_salarie2_nom
)
)
)
)
)
You can see the json code:
{"1":{"text":"e1","children":{"1":{"text":"e1_site1","children":{"1":"e1_site1_salarie1_nom"}},"3":{"text":"e1_site2","children":{"3":"e1_site2_sa1","4":"e1_site2_sa2"}}}},"2":{"text":"e2","children":{"2":{"text":"e2_site2","children":{"2":"e2_site2_salarie2_nom"}}}}}
I want the result to be like this:
{{"text":"e1","children":{{"text":"e1_site1","children":{"text":"e1_site1_salarie1_nom"}},{"text":"e1_site2","children":{"text":"e1_site2_sa1","text":"e1_site2_sa2"}}}},{"text":"e2","children":{{"text":"e2_site2","children":{"text":"e2_site2_salarie2_nom"}}}}}