How to transform json data to a comma separated php string?

Go To StackoverFlow.com

2

Let's say i have this json data. How to transform the "tags" to a string like

$tags = "Rihanna, We, Found, Love, (Explicit), Def, Jam, Records, Pop"; ?

{ "apiVersion" : "2.1",
      "data" : { "items" : [ { "accessControl" : { "autoPlay" : "allowed",
                    "comment" : "allowed",
                    "commentVote" : "allowed",
                    "embed" : "allowed",
                    "list" : "allowed",
                    "rate" : "allowed",
                    "syndicate" : "allowed",
                    "videoRespond" : "allowed"
                  },
                "aspectRatio" : "widescreen",
                "category" : "Music",
                "tags" : [ "Rihanna",
                    "We",
                    "Found",
                    "Love",
                    "(Explicit)",
                    "Def",
                    "Jam",
                    "Records",
                    "Pop"
                  ],
                "title" : "Rihanna - We Found Love ft. Calvin Harris"
              } ],
          "itemsPerPage" : 1,
          "startIndex" : 1,
          "totalItems" : 859012,
          "updated" : "2012-04-04T20:32:26.170Z"
        }
    }

For the title as example, the script looks like this:

$content = $this->getDataFromUrl($feedURL);
$content = json_decode($content,true);

$videosList = $content['data']['items'];

for($i=0; $i<count($videosList); $i++) {

$videosDatas['videos'][$i]['title'] = $videosList[$i]['title'];

}
2012-04-04 20:42
by m3tsys
Do you want tags written to $videosDatas['videos'][$i]['tags'] - iambriansreed 2012-04-04 21:01


5

It looks like you need implode(). The function would be something like...

$tags = implode(', ', $videosDatas['videos'][$i]['tags']);
2012-04-04 20:47
by Espilon
you need to change $tags to 'tags - binarious 2012-04-04 21:01
Thanks binarious - Espilon 2012-04-04 22:44
thank you very much - m3tsys 2012-04-05 20:50


2

$comma_sep_string = implode(', ' , $videosDatas['videos'][$i]['tags']);
2012-04-04 20:46
by binarious


0

Try:

foreach($videosList as $i=>$video){
    $videosDatas['videos'][$i]['title'] = $video->title;
    $tags = implode(', ',$video->tags);
    $videosDatas['videos'][$i]['tags'] = $tags;
}

In place of your code:

for($i=0; $i<count($videosList); $i++) {

$videosDatas['videos'][$i]['title'] = $videosList[$i]['title'];

}
2012-04-04 20:52
by iambriansreed
Ads