I need to get an array output as this [[1, 26], [3, 16], [3, 17], [4, 27], [4, 26]] from the following php mysql code
include("db.php");
$villa = $_GET['villa'];
$sql = mysql_query("SELECT unavailable_date_from, unavailable_date_to FROM villa WHERE name = '".$villa."'");
$json = "[";
while($results = mysql_fetch_object($sql)){
foreach ($results as $field => $value) {
$a = explode("-", $value);
$json = $json . '[' . $a[1] .','. $a[2] .'],';
}
$json = substr($json, 0, strlen($json)-1);
}
$json = $json . ']';
echo $json;
The current code give the following result: [[03,08],[03,10][03,15],[03,20]]
Can somebody tell me how can I add that one comma between the second and third array element
Thanks
Move
$json = substr($json, 0, strlen($json)-1);to be on the line before$json = $json . ']';.It will look like:
Kind of messy, but it will work. Edit: Using
rtrimwould be nicer:Here’s the full code: