0

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();
}
3
  • @RohitKumar updated my post Commented Oct 14, 2015 at 11:03
  • try this $resultArray = array(); instead of $resultArray = null; Commented Oct 14, 2015 at 11:19
  • How are you accessing the $resultArray in your JS? Do you mind posting that code too? Commented Oct 14, 2015 at 11:19

2 Answers 2

1

When you use json_encode it automaically converts array to objects i.e javascript object notation ..No need to manipulate .Only you have to do it as --

if ($count > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $resultArray[]=$row;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Leverages the fact that SELECT * wasn't used. +1
0

One way to fix it is: You could index in the array (to get the array in the array) and store its value in a new variable. This way you will get rid of the "double array".

Comments