I am trying to figure out how to create (subarrays)? I'm not sure if that is the correct term, but I have examples listed below.
Essentially, I want an array for each resort. Within that array, I want array/sub-arrays for each room.
Any help is appreciated.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbh = new PDO("mysql:host=$servername;dbname=checkavail", $username, $password);
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$occupants = $_POST['occupants'];
$sql=
"SELECT
MIN(staydate) AS checkin,
MAX(staydate) AS lastnight,
MIN(available) AS available,
ra.resort AS resortcode,
ri.resort AS resortname,
ri.room AS roomname,
ri.roomcode AS roomcode,
ri.view AS viewname,
SUM(ra.points) AS points,
ri.sqfoot AS sqfoot,
ri.description AS roomdescription,
ri.bedding AS bedding,
ri.amenities AS amenities,
ri.sleeps AS sleeps,
ri.sleep_details AS sleepdetails,
ri.layout_img AS layoutimg,
ri.room_img AS roomimg,
ri.roomimg_thumb AS roomimgthumb
FROM resort_availability AS ra
LEFT JOIN room_info AS ri ON (ra.room = ri.roomcode)
WHERE staydate >= '$checkin' AND staydate < '$checkout'
AND sleeps >= '$occupants'
GROUP BY resortname, roomname
ORDER BY points
";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$return = [];
foreach ($result as $row) {
$points = $row['points'];
$price = $points*20;
$pricefrmt = "$".number_format ($price, 2);
$return[] = [
'resort' => $row['resortname'],
'room' => $row['roomname'],
'price' => $pricefrmt
];
}
$dbh = null;
header('Content-type: application/json');
echo json_encode($return);
?>
This is the current output:
[
{
"resort":"Resort1",
"room":"DStudio",
"price":"$2,560.00"
},
{
"resort":"Resort1",
"room":"TVilla",
"price":"$3,980.00"
},
{
"resort":"Resort1",
"room":"1-Bedroom Villa",
"price":"$5,460.00"
},
{
"resort":"Resort1",
"room":"2-Bedroom LO Villa",
"price":"$7,040.00"
},
{
"resort":"Resort1",
"room":"2-Bedroom Villa",
"price":"$7,040.00"
},
{
"resort":"Resort1",
"room":"3-Bedroom Villa",
"price":"$15,620.00"
},
{
"resort":"Resort2",
"room":"DStudio",
"price":"$2,560.00"
},
{
"resort":"Resort2",
"room":"TVilla",
"price":"$3,980.00"
},
{
"resort":"Resort2",
"room":"1-Bedroom Villa",
"price":"$5,460.00"
},
{
"resort":"Resort2",
"room":"2-Bedroom LO Villa",
"price":"$7,040.00"
},
{
"resort":"Resort2",
"room":"2-Bedroom Villa",
"price":"$7,040.00"
},
{
"resort":"Resort2",
"room":"3-Bedroom Villa",
"price":"$15,620.00"
}
]
But I would like for it to look more like this... (if the formatting seems a bit weird it's because I am a noob at this)
[
{
"resort":"Resort1"
[
{
"room":"DStudio",
"price":"$2,560.00"
},
{
"room":"TVilla",
"price":"$3,980.00"
},
{
"room":"1-Bedroom Villa",
"price":"$5,460.00"
},
{
"room":"2-Bedroom LO Villa",
"price":"$7,040.00"
},
{
"room":"2-Bedroom Villa",
"price":"$7,040.00"
},
{
"room":"3-Bedroom Villa",
"price":"$15,620.00"
}
]
}
{
"resort":"Resort2"
[
{
"room":"DStudio",
"price":"$2,560.00"
},
{
"room":"TVilla",
"price":"$3,980.00"
},
{
"room":"1-Bedroom Villa",
"price":"$5,460.00"
},
{
"room":"2-Bedroom LO Villa",
"price":"$7,040.00"
},
{
"room":"2-Bedroom Villa",
"price":"$7,040.00"
},
{
"room":"3-Bedroom Villa",
"price":"$15,620.00"
}
]
}
]