I want to create a Array with multidimension arrays from a database. The Database has 3 tables, one for vehicle, one for damages and one for damagesPhotos.
Table vehicle has two columns id and name
Table damages has four columns damagesID, vehicleID, damagesType and damagesDescription.
Table damagesPhotos has three columns damagesPhotoID, damagesID and damagesPhotoUrl
I need to combine thoose three columns into an array, that looks like this:
$vehicle = array(
"id" => "somestring",
"name" => "somestring",
"damages" => array(
"damagesType" => "somestring",
"damagesDescription" => "somestring",
"photoOfDamages" => array(
"damagesPhotoUrl" => "somestring"
)
)
);
I'am using this code:
$query = "SELECT * from vehicle v LEFT JOIN damages d ON v.id = d.vehicleID LEFT JOIN damagesPhotos p ON d.damagesID = p.damagesID WHERE d.damagesID = p.damagesID AND v.id = 1";
$result = mysql_query($query);
$i = 0;
$vehicle = array();
while($row = mysql_fetch_array($result)){
$vehicle[$i] = array(
"id" => $row[id],
"name" => $row[name],
"damages" => array(
"damagesType" => $row[damagesType],
"damagesDescription" => $row[damagesDescription],
"photoOfDamages" => array(
"damagesPhotoUrl" => $row[damagesPhotoUrl]
)
)
);
$i++;
}
And it returns this:
[{"vehilceId":"1",
"name":"AW55005",
"damages":{
"damagesType":"Exterior",
"damagesDescription":"Rust",
"photoOfDamages":{
"damagesPhotoUrl":"link to damagesPhoto 01"
}
}
},
{"vehilceId":"1",
"name":"AW55005",
"damages":{
"damagesType":"Exterior",
"damagesDescription":"Rust",
"photoOfDamages":{
"damagesPhotoUrl":"link to damagesPhoto 02"
}
}
},
{"vehilceId":"1",
"name":"AW55005",
"damages":{
"damagesType":"Interior",
"damagesDescription":"Scratch",
"photoOfDamages":{
"damagesPhotoUrl":"link to damagesPhoto 03"
}
}
}
But as you can see the first two objects are the same only the damagesPhotoUrl is different. How do I merge thoose two array so it will look like this:
{"vehilceId":"1",
"name":"AW55005",
"damages":{
"damagesType":"Exterior",
"damagesDescription":"Rust",
"photoOfDamages":{
{"damagesPhotoUrl":"link to damagesPhoto 01"},
{"damagesPhotoUrl":"link to damagesPhoto 02"}
}
}
}, ...
Thanks in advance.
/ Morten