获取,解码和重新编码JSON

I am attempting to fetch JSON from Instagram according to a number of URL parameters, the JSON is then decoded and then the objects required are then encoded into my own JSON format. Whilst I know this sound a little ridiculous, it is what is required. My only issue here is that for some reason it does not encode each section of JSON, it will only work for one item. The code is as below.

<?php

function instagram($count=16){

$igtoken = $_GET['igtoken'];
$hashtag = $_GET['hashtag'];


    $url = 'https://api.instagram.com/v1/tags/'.$hashtag.'/media/recent/?access_token='.$igtoken.'&count='.$count;

        $jsonData = json_decode((file_get_contents($url)));

$jsonData = json_decode((file_get_contents($url)));
    foreach ($jsonData->data as $key=>$value) {

            $response = array();
            $response["data"] = array();
            $data = array();
            $data["createdtime"] = $value->caption->created_time;
            $data["username"] = $value->caption->from->username;
            $data["profileimage"] = $value->caption->from->profile_picture;
            $data["caption"] = $value->caption->text;
            $data["postimage"] = $value->images->standard_resolution->url;

            array_push($response["data"], $data);
            $result = json_encode($response); 

    }

    return $result;
}

echo instagram();
?>

It will work for each section of JSON if I do something like this instead:

    $result .= '<li> 
                      '.$value->caption->from->username.'<br/>
                      '.$value->caption->from->profile_picture.'<br/>
                      '.$value->caption->text.'<br/>
                      '.$value->images->standard_resolution->url.'<br/>
                      '.$value->caption->created_time.'<br/>
                      </li>';

I feel I have bodged up somewhere with the array, however i'm not entirely sure.

What if we move $response["data"] and $result varia out of foreach?

Have you tried this?

$response = array();
$response["data"] = array();
foreach ($jsonData->data as $key=>$value) {            

        $data = array();
        $data["createdtime"] = $value->caption->created_time;
        $data["username"] = $value->caption->from->username;
        $data["profileimage"] = $value->caption->from->profile_picture;
        $data["caption"] = $value->caption->text;
        $data["postimage"] = $value->images->standard_resolution->url;

        array_push($response["data"], $data);
}
$result = json_encode($response); 
return $result;