-1

I want to echo JSON array with the title Trailers like I have shown in the example code I am getting a list of movies from my database and parsing it into an array this is my current code

while($row = mysqli_fetch_assoc($result)){
        $vid = $row['vid'];
        $cpath = $row['cpath'];
        $vpath = $row['vpath'];
        $title = $row['title'];
        $description = $row['description'];
        
        array_push($array,["title"=>$title,"description"=>$description,"videoID"=>$vid,"thumbnail"=>$cpath,"videoUrl"=>$vpath]);

    }

    echo json_encode($array);

this is the out put

[
{
    "title": "Coming Soon",
    "description": "Coming Soon",
    "videoID": "cb55e7345g8d0e2",
    "thumbnail": "https://dl.dropboxusercontent.com/s/w6zjbbes173o551/.webp?dl=0",
    "videoUrl": "../uploads/videos/documentaries/.mp4"
},
{
    "title": "Overblown",
    "description": "",
    "videoID": "c5caf7028ffac",
    "thumbnail": "https://dl.dropboxusercontent.com/s//OverBlownCover.webp?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s//%20%28Official%20Trailer%29%20Done.mp4?dl=0"
},
{
    "title": "BloodLine",
    "description": "A girl falls in love with two gentlemen of which one is the younger brother for her first man.",
    "videoID": "ffssssssssss",
    "thumbnail": "https://dl.dropboxusercontent.com/s//bloodline.png?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/qxw8wcno643rvwp/.mp4?dl=0"
},
{
    "title": "Drastic",
    "description": "Drastic ",
    "videoID": "4eeb9c5008b75fds62",
    "thumbnail": "https://dl.dropboxusercontent.com/s/dr9m4njmsdsr4nn4b4/cover_Drastic.webp?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/r28l0ldddc8p6w9/DRASTIC%sd%20Trailer.mp4?dl=0"
},
{
    "title": "Young Widow",
    "description": "Jane has just lost her husband and despite being urged to cry, to mourn her husband, Jane is still in denial of his death. While asleep...",
    "videoID": "bb8e0a30d898sd6cfc",
    "thumbnail": "https://dl.dropboxusercontent.com/s/moj6m2oresd86er09/young%20widow%20poster%202.jpg?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/4rsmzlwtzfsfsdvnb/young%20widow%20trailer.mp4?dl=0"
}

]

I want it to echo this

{"trailers": [
{
    "title": "Coming Soon",
    "description": "Coming Soon",
    "videoID": "cb55e7345g8d0e2",
    "thumbnail": "https://dl.dropboxusercontent.com/s/w6zjbbes173o551/.webp?dl=0",
    "videoUrl": "../uploads/videos/documentaries/.mp4"
},
{
    "title": "Overblown",
    "description": "",
    "videoID": "c5caf7028ffac",
    "thumbnail": "https://dl.dropboxusercontent.com/s//OverBlownCover.webp?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s//%20%28Official%20Trailer%29%20Done.mp4?dl=0"
},
{
    "title": "BloodLine",
    "description": "A girl falls in love with two gentlemen of which one is the younger brother for her first man.",
    "videoID": "ffssssssssss",
    "thumbnail": "https://dl.dropboxusercontent.com/s//bloodline.png?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/qxw8wcno643rvwp/.mp4?dl=0"
},
{
    "title": "Drastic",
    "description": "Drastic ",
    "videoID": "4eeb9c5008b75fds62",
    "thumbnail": "https://dl.dropboxusercontent.com/s/dr9m4njmsdsr4nn4b4/cover_Drastic.webp?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/r28l0ldddc8p6w9/DRASTIC%sd%20Trailer.mp4?dl=0"
},
{
    "title": "Young Widow",
    "description": "Jane has just lost her husband and despite being urged to cry, to mourn her husband, Jane is still in denial of his death. While asleep...",
    "videoID": "bb8e0a30d898sd6cfc",
    "thumbnail": "https://dl.dropboxusercontent.com/s/moj6m2oresd86er09/young%20widow%20poster%202.jpg?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/4rsmzlwtzfsfsdvnb/young%20widow%20trailer.mp4?dl=0"
}

]}

I need the echo Json to be output with the title Trailers and exactly the way I've shown an example code.

1 Answers1

0
$result = (object)[];
$result->trailers = [];

while($row = mysqli_fetch_assoc($result)){
        $vid = $row['vid'];
        $cpath = $row['cpath'];
        $vpath = $row['vpath'];
        $title = $row['title'];
        $description = $row['description'];
        array_push($result->trailers,["title"=>$title,"description"=>$description,"videoID"=>$vid,"thumbnail"=>$cpath,"videoUrl"=>$vpath]);

    }

    echo json_encode($result);
Dharman
  • 30,962
  • 25
  • 85
  • 135
khanam
  • 89
  • 5
  • 1
    Please add some explanation to your answer such that others can learn from it – Nico Haase Sep 21 '22 at 08:28
  • There's no need to cast the array into an object. It's just unnecessary clutter. – M. Eriksson Sep 21 '22 at 08:28
  • @M.Eriksson yes, we have many ways to solve a problem, i hope with my solution people will see something new here – khanam Sep 21 '22 at 08:30
  • I think you missed the point. There's no need to add unnecessary code and operations. Good code is simple code (specially when answering a question to someone that seem to be less experienced). – M. Eriksson Sep 21 '22 at 08:57
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 26 '22 at 07:49