Mr. V Mr. V - 6 months ago 27
JSON Question

PHP foreach array IDs

I have a foreach loop that is supposed to loop through JSON and return the appropriate ID of each video listed in JSON using the Youtube api.
Here is my code:

class Videos {
private $mVideoUrl;

function setVideoTitle($videoUrl){
$this->mVideoUrl= $videoUrl;
}

function getVideoTitle(){
return $this->mVideoUrl;
}
}

$jsonFile = file_get_contents($url);
$jfo = json_decode($jsonFile);
$items = $jfo->items;
$vidArray = array();

foreach ($items as $item){
if(!empty($item->id->videoId)){
$Videos = new Videos;
$Videos->setVideoUrl($item->id->videoId);
$id = $Videos->getVideoUrl();
array_push($vidArray, $id);
}
echo $vidArray[0];
}


Problem is, the array push is working correctly, but it is only adding the 1st ID in the list only for each loop iteration when i echo it. When I echo the $id variable, it prints all IDs just fine.

Ultimately i want to be able to create an object for each video, storing it's ID and other information.

I feel like this is a simple fix but i can't figure it out for the life of me.
I would appreciate any help!
Also if i am going about this all wrong, advice is appreciated as well!

Thanks!

Answer

I've played little bit with your code. I've modified your class. I've renamed plurar Videos to Video (singular).

Then I've added an attribute $id, because name of properties should be simple and represent the data we want to store in them.

Then I've added getter and setter for the $id property.

I don't know the $url, so I've just write simple JSON string instead. I tried to mimic the structure that you're using in your code.

Then I've added () to the end of new Video() to have proper constructor called.

And instead of pushing elements into array, I'm using proper $array[$index] = assignment.

Last thing, I've moved writing out the data out of the foreach-cycle. And I'm using var_export to get proper php code to play with if redirected to another file.

<?php

class Video
{
    private $mVideoUrl;
    private $id; // added id attribute

    /**
     * @return mixed
     */
    public function getId() // added getter
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id) // added setter
    {
        $this->id = $id;
    }


    function setVideoTitle($videoUrl)
    {
        $this->mVideoUrl = $videoUrl;
    }

    function getVideoTitle()
    {
        return $this->mVideoUrl;
    }
}

// ignored for now
// $jsonFile = file_get_contents($url);
$jsonFile = '{"items": [
        { "id": { "videoId": 1, "url": "http://www.youtube.com/1" } },
        { "id": { "videoId": 2, "url": "http://www.youtube.com/2" } },
        { "id": { "videoId": 3, "url": "http://www.youtube.com/3" } },
        { "id": { "videoId": 4, "url": "http://www.youtube.com/4" } },
        { "id": { "videoId": 5, "url": "http://www.youtube.com/5" } }
    ]
}';

$jfo = json_decode($jsonFile);

$items = $jfo->items;
$vidArray = array();

foreach ($items as $item)
{
    if (!empty($item->id->videoId))
    {
        $Video = new Video(); // added brackets

        $Video->setId($item->id->videoId); // changed to setId
        $Video->setVideoTitle($item->id->url);
        $id = $Video->getId();
        $vidArray[$id] = $Video;
    }

}

// write out all data
var_export($vidArray);
Comments