Php 在一行中打印JSON数据时出现问题
我有JSON数据,需要在一行中打印类别,但类别有多个值 我试图导出标签,但我坚持在一行中打印值Php 在一行中打印JSON数据时出现问题,php,json,Php,Json,我有JSON数据,需要在一行中打印类别,但类别有多个值 我试图导出标签,但我坚持在一行中打印值 “类别”打印文本中的示例:类别1、类别2、类别3、类别4 我试着吼叫,但不起作用: foreach ($jf['data']['feed'] as $item1) { $item = $item1['article']; foreach($item['categories'] as $categories1) { echo $categories123; } JSON文件 {
“类别”
打印文本中的示例:类别1、类别2、类别3、类别4
我试着吼叫,但不起作用:
foreach ($jf['data']['feed'] as $item1) {
$item = $item1['article'];
foreach($item['categories'] as $categories1) {
echo $categories123;
}
JSON文件
{
"status": "success",
"data": {
"feed": [
{
"article": {
"id": "165354",
"title": "Title1",
"image_url": "example.com/image1.png",
"native_app_url": "example.com/article1",
"article_url": "example.com/article1",
"article_type": "Article",
"created_at": "2020-01-15T16:55:01Z",
"featured_time": "2020-02-19T17:19:33Z",
"author_name": "2",
"matches": [
],
"categories": [
"Categories1",
"Categories2",
"Categories3",
"Categories4"
],
"tags": [
"Tags1",
"Tags2",
"Tags3"
],
"embed_code": "<embed>12453</embed>",
"synd_code": "<embed>121245</embed>"
}
},
{
"article": {
"id": "112354",
"title": "Title2",
"image_url": "example.com/image2.png",
"native_app_url": "example.com/article2",
"article_url": "example.com/article2",
"article_type": "Article",
"created_at": "2020-01-11T16:55:01Z",
"featured_time": "2020-01-19T17:19:33Z",
"author_name": "1",
"matches": [
],
"categories": [
"Categories1",
"Categories2",
"Categories3",
"Categories4"
],
"tags": [
"Tags1",
"Tags2",
"Tags3"
],
"embed_code": "<embed>123</embed>",
"synd_code": "<embed>12345</embed>"
}
},
{
"article": {
...
}
}
],
"next_page": 2
},
"message": ""
}
{
“状态”:“成功”,
“数据”:{
“提要”:[
{
“第条”:{
“id”:“165354”,
“标题”:“标题1”,
“image_url”:“example.com/image1.png”,
“本地应用程序url”:“example.com/article1”,
“文章url”:“example.com/article1”,
“物品类型”:“物品”,
“创建时间”:“2020-01-15T16:55:01Z”,
“特色时间”:“2020-02-19T17:19:33Z”,
“作者姓名”:“2”,
“匹配项”:[
],
“类别”:[
“类别1”,
“类别2”,
“类别3”,
“类别4”
],
“标签”:[
“标记1”,
“标记2”,
“标记3”
],
“嵌入代码”:“12453”,
“synd_代码”:“121245”
}
},
{
“第条”:{
“id”:“112354”,
“标题”:“标题2”,
“image_url”:“example.com/image2.png”,
“本地应用程序url”:“example.com/article2”,
“文章url”:“example.com/article2”,
“物品类型”:“物品”,
“创建时间”:“2020-01-11T16:55:01Z”,
“特色时间”:“2020-01-19T17:19:33Z”,
“作者姓名”:“1”,
“匹配项”:[
],
“类别”:[
“类别1”,
“类别2”,
“类别3”,
“类别4”
],
“标签”:[
“标记1”,
“标记2”,
“标记3”
],
“嵌入代码”:“123”,
“同步代码”:“12345”
}
},
{
“第条”:{
...
}
}
],
“下一页”:2
},
“消息”:”
}
使用内爆
在数组元素之间插入分隔符
$categories = implode(", ", $item['categories']);
如果只需要有限的数量,请在内爆之前对阵列进行切片
$category_limit = 4; // only show first 4 categories
$categories = implode(", ", array_slice($item['categories'], 0, $category_limit));
大家好,我的JSON脚本中还有一个问题,我将JSON提要“article_url”中的新函数链接添加到我的RSS提要作为标记。我添加函数
if(isset($item['article\u url'))$newItem->addChild('link',$item['article\u url')代码>但是得到输出我检查了JSON提要,我看到在键“article\u url”中有链接@BarmarIt如果(!empty($item['article\u url'])
最好使用。这将跳过空字符串。