Home > Net >  how can I return php array in an object?
how can I return php array in an object?

Time:09-21

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// (Official Trailer) Done.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 Trailer.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 widow poster 2.jpg?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/4rsmzlwtzfsfsdvnb/young widow trailer.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// (Official Trailer) Done.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 Trailer.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 widow poster 2.jpg?dl=0",
    "videoUrl": "https://dl.dropboxusercontent.com/s/4rsmzlwtzfsfsdvnb/young widow trailer.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.

CodePudding user response:

$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);
  • Related