JSON array in array from SQL database using PHP -
i have following code:
$retarray = array(); $hoursarray = array(); $res = $this->mysqli->query("select id, title, groupby scheduler_activity gewerbe = '$this->gewerbe'"); while($row = $res->fetch_object()) { $foo = $this->mysqli->query("select dow, start, end scheduler_businesshours activity = '$row->id'"); while($hours = $foo->fetch_object()){ $hoursarray[] = $hours; } $retarray[] = $row; $retarray["businesshours"] = $hoursarray; } return $retarray;
and following json output:
{ "0": { "id": "1", "title": "spinning", "groupby": "trainingsgruppe" }, "businesshours": [{ "dow": "[1,2,3]", "start": "17:00:00", "end": "18:00:00" }, { "dow": "[4,5]", "start": "17:30:00", "end": "18:30:00" }], "1": { "id": "2", "title": "massage", "groupby": "trainingsgruppe" }, "2": { "id": "3", "title": "yoga", "groupby": "trainingsgruppe" }, "3": { "id": "4", "title": "dance academy", "groupby": "trainingsgruppe" }
}
so far looks correct need rid of leading numbers 0, 1, 2, 3
that plus businesshours:
[{ "id": "1", "title": "spinning", "groupby": "trainingsgruppe" }, { "id": "2", "title": "massage", "groupby": "trainingsgruppe" }, { "id": "3", "title": "yoga", "groupby": "trainingsgruppe" }, { "id": "4", "title": "dance academy", "groupby": "trainingsgruppe" }]
what did wrong there?
thank you!
as understood code, business hour each activity, it's small change @ line 10:
$retarray = array(); $hoursarray = array(); $res = $this->mysqli->query("select id, title, groupby scheduler_activity gewerbe = '$this->gewerbe'"); while($row = $res->fetch_object()) { $foo = $this->mysqli->query("select dow, start, end scheduler_businesshours activity = '$row->id'"); while($hours = $foo->fetch_object()){ $hoursarray[] = $hours; } $row["businesshours"] = $hoursarray; // change this, swap line 10 & 11 $retarray[] = $row; } return $retarray;
hope helps.
Comments
Post a Comment