3

このjsonデータがあるとしましょう。「タグ」を次のような文字列に変換する方法

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

例としてのタイトルの場合、スクリプトは次のようになります。

$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'];

}
4

3 に答える 3

8

implode()が必要なようです。関数は次のようになります...

$tags = implode(', ', $videosDatas['videos'][$i]['tags']);
于 2012-04-04T20:47:08.623 に答える
2
$comma_sep_string = implode(', ' , $videosDatas['videos'][$i]['tags']);
于 2012-04-04T20:46:18.520 に答える
0

試す:

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

コードの代わりに:

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

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

}
于 2012-04-04T20:52:52.317 に答える