如何将json数据转换为逗号分隔的php字符串?

假设我有这个json数据。 如何将“标签”转换为字符串,如</ p>

$ tags =“Rihanna,We,Found,Love,(Explicit),Def,Jam,Records,Pop”; </ code>?</ p>

  {“apiVersion”:“2.1”,
“data”:{“items”:[{“accessControl”:{“autoPlay”: “允许”,
“注释”:“允许”,
“”注释投票“:”允许“,
”嵌入“:”允许“,
”列表“:”允许“,
”费率“: “允许”,
“辛迪加”:“允许”,
“视频响应”:“允许”
},
“aspectRatio”:“宽屏”,
“类别”:“音乐”,
“ 标签“:[”Rihanna“,
”我们“,
”找到“,
”爱“,
”(明确)“,
”Def“,
”Jam“,
”记录 “,
”Pop“
],
”title“ :“蕾哈娜 - 我们发现爱情。凯文哈里斯”
}],
“itemsPerPage”:1,
“startIndex”:1,
“totalItems”:859012,
“更新”:“2012- 04-04T20:32:26.170Z“
}
}
</ code> </ pre>

对于标题示例,脚本如下所示:</ p>

<前> $ content = $ this-&gt; getDataFromUrl($ feedURL);
$ content = json_decode($ content,true);

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

有关($ I = 0; $ I&LT;计数($ videosList); $ i ++){

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

}
</ code> </ pre>
</ div>

展开原文

原文

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

}

douqin6785
douqin6785 你想要标签写入$videosDatas['videos'][$i]['tags']吗?
8 年多之前 回复

3个回答



看起来你需要破灭()。 该函数类似于...... </ p>

  $ tags = implode(',',$ videosDatas ['videos'] [$ i] ['tags']);

</ code> </ pre>
</ div>

展开原文

原文

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

$tags = implode(', ', $videosDatas['videos'][$i]['tags']);

dsxi70423
dsxi70423 谢谢你。
8 年多之前 回复
donglv6747
donglv6747 你需要将$标签更改为'标签'
8 年多之前 回复



  $ comma_sep_string = implode(',',$ videosDatas ['videos'] [$ i] ['tags']  ); 
</ code> </ pre>
</ div>

展开原文

原文

$comma_sep_string = implode(', ' , $videosDatas['videos'][$i]['tags']);



尝试:</ p>

  foreach($ videosList as $ i =&gt; $ video)  ){
$ videosDatas ['videos'] [$ i] ['title'] = $ video-&gt; title;
$ tags = implode(',',$ video-&gt; tags);
$ videosDatas ['videos'] [$ i] ['tags'] = $ tags;
}
</ code> </ pre>

代替您的代码:</ p> \ n

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

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

}
</ code> </ pre>
</ div>

展开原文

原文

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

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐