0

I'm attempting to output my JSON in a format just like this: http://api.androidhive.info/json/movies.json and I am unsure of how to accomplish this through my PHP output code. Right now, It is displaying with "post" at the top and is a map, which I don't know how to successfully remove, seen here: http://shipstudent.com/complaint_desk/androidfriendsList.php?username=noah. Please let me know if you need more information.

PHP:

$rows = $stmt->fetchAll();


if ($rows) {

    $response["posts"]   = array();

    foreach ($rows as $row) 
    {
        $post = array();
        $post["username"] = $row["username"];
        $post["profile_picture"] = $row["profile_picture"];

        array_push($response["posts"], $post);
    }

    // echoing JSON response
    echo json_encode($response);


} else 
{   
    die(json_encode($response));
}

JSON:

{
    "posts": [
        {
            "username": "noah",
            "profile_picture": "https://shipstudent.com/animal/appphotos/978321177.jpg"
        },
        {
            "username": "e",
            "profile_picture": "https://shipstudent.com/complaint_desk/appphotos/owl.jpeg"
        },
    ]
}

Desired format:

[{
        "title": "Dawn of the Planet of the Apes",
        "image": "http://api.androidhive.info/json/movies/1.jpg",
        "rating": 8.3,
        "releaseYear": 2014,
        "genre": ["Action", "Drama", "Sci-Fi"]
    },
    {
        "title": "District 9",
        "image": "http://api.androidhive.info/json/movies/2.jpg",
        "rating": 8,
        "releaseYear": 2009,
        "genre": ["Action", "Sci-Fi", "Thriller"]
    },
0

2 Answers 2

3
$rows = $stmt->fetchAll();

$response = [];

foreach ($rows as $row) {
    $post = [
        "username" => $row["username"],
        "profile_picture" => $row["profile_picture"]
    ];
    $response[] = $post;
}

echo json_encode($response);
Sign up to request clarification or add additional context in comments.

3 Comments

Almost, I keep get the error "syntax error, unexpected '['" at $response = [];
what is your version of php? if less than 5.4, then use the old syntax for arrays: $response = array(); $post = array("username" => $row["username"], "profile_picture" => $row["profile_picture"]);
And my PHP version is 5.5
0
$response = array();

foreach ($rows as $row) 
{
    $post = array();
    $post["title"] => // define title;
    $post["image"] => // define image;
    $post["rating"] => // define rating;
    $post["releaseYear"] => // define releaseYear;
    $post["genre"] => // define genre, must be array();
    array_push($response, $post);
}

1 Comment

This would not work due to it saying unexpected => on each line there and when removing it, the server would say unexpected variable...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.