Im trying to create and array of objects in php to send to my AngularJS app the format want is
[
{},
{},
{}
]
but when i run my code i get an outer array and a inner array and the objects are inside the inner array..i dont want 2 arrays just a single outer level array
heres what im getting
[
[
{
"id": "16",
"post_title": "Gotta love Batman",
"author_name": "Clinton Dsouza",
"publish_date": "Tuesday, October 13th 2015, 4:50:50 pm"
},
{
"id": "15",
"post_title": "Web 2.0 ipsum...Again",
"author_name": "Clinton Dsouza",
"publish_date": "Tuesday, October 13th 2015, 4:48:42 pm"
},
{
"id": "10",
"post_title": "Vegetable Ipsum",
"author_name": "Clinton Dsouza",
"publish_date": "Tuesday, October 13th 2015, 1:36:57 pm"
},
{
"id": "9",
"post_title": "Yet another Harry Potter ipsum",
"author_name": "Clinton Dsouza",
"publish_date": "Tuesday, October 13th 2015, 1:28:55 pm"
}
]
]
as u can see there's an array inside an array..how do i go about fixing this to obtain the format i want
my code
try {
$resultArray = null;
$sql = "SELECT id,post_title,author_name,publish_date FROM posts ORDER BY id DESC LIMIT 4 OFFSET $data->offset ";
$result = mysql_query($sql) or trigger_error(mysql_error() . $sql);
$count = mysql_num_rows($result);
$index = 0;
if ($count > 0) {
while ($row = mysql_fetch_assoc($result)) {
$resultArray[$index]=new StdClass();
$resultArray[$index]->id = $row['id'];
$resultArray[$index]->post_title = $row['post_title'];
$resultArray[$index]->author_name = $row['author_name'];
$resultArray[$index]->publish_date = $row['publish_date'];
$index++;
}
$response['status'] = 'Success';
$response['message'] = 'Data present';
$response['results'] = $resultArray;
} else {
$response['status'] = 'Error';
$response['message'] = 'No Posts found';
}
echo json_encode($response);
} catch (Exception $e) {
$response['status'] = 'Error';
$response['message'] = $e->getMessage();
echo json_encode($response);
die();
}
$resultArray = array();instead of$resultArray = null;