Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I return php array in an object?

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"
}

]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

$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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading