0

I'm new to JSON and trying to join two PHP arrays and output the result as JSON. I ran the output into a JSON validator and it said it was ok but I'm not convinced. How can I replace the "0" on line 4 of the output to be "genres" and will the output be easy to pull out each genre name by the receiver?

{
"apiVersion": "1.0",
"status": "ok",
"0": [
    {
        "id": 0,
        "name": "Miscellaneous",
        "like": "NO"
    },
    {
        "id": 1,
        "name": "Breakbeat",
        "like": "NO"
    },
    {
        "id": 2,
        "name": "Trance",
        "like": "NO"
    },  etc....

Here is my code;

    $data = array(
    "apiVersion"=>"1.0",
    "status"=> "ok"
);

$i = 0;
foreach ($exploded as $item) {
    $e_row[] = array(
        "id" => $i,
        "name" => $item,
        "like" => "NO"     
    );
    $i = $i + 1;
} 

array_push($data,$e_row);

header('Content-type:application/json;charset=utf-8');
echo json_encode($data, JSON_PRETTY_PRINT);  
3
  • 1
    array_push($data,$e_row); => $data['genres'] = $e_row; Commented Dec 5, 2020 at 18:37
  • Actually you don't need that if you do $data['genres'][] = array( in the loop. Commented Dec 5, 2020 at 18:40
  • And what does this mean will the output be easy to pull out each genre name by the receiver? Commented Dec 5, 2020 at 18:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.