Php 如何以几乎所有内容都存储为数组的格式从Json数据中检索信息?

Php 如何以几乎所有内容都存储为数组的格式从Json数据中检索信息?,php,json,scrapy,Php,Json,Scrapy,我目前正在使用爬行器在reddit中爬行,并以json格式为我提供链接和标题。但是,在创建json文件时,数据存储为单个数组,如下所示: [{"url": ["http://i.imgur.com/1Pw3ehZ.jpg"], "title": ["Sunset in Trinidad, CA", "bungholesex"]}, {"url": ["http://i.imgur.com/neQiFcf.jpg"], "title": ["Humboldt Bay, Eureka Califor

我目前正在使用爬行器在reddit中爬行,并以json格式为我提供链接和标题。但是,在创建json文件时,数据存储为单个数组,如下所示:

[{"url": ["http://i.imgur.com/1Pw3ehZ.jpg"], "title": ["Sunset in Trinidad, CA", "bungholesex"]},
{"url": ["http://i.imgur.com/neQiFcf.jpg"], "title": ["Humboldt Bay, Eureka California", "bungholesex"]},
{"url": ["http://imgur.com/dxKHGLV"], "title": ["Crater Lake, Oregon in April", "CausticRain11"]},
{"url": ["http://imgur.com/RPv475F"], "title": ["South Dakota is seriously beautiful.", "ratt1601"]},
{"url": ["http://imgur.com/oH1u7nk"], "title": ["The view outside my back door. Virginia is alright, I guess...", "toowhitetofail"]},

每当我去检索和回显用json_解码器转换的数据时,我都会收到一条通知,上面写着“数组到字符串转换”,但没有显示任何数据。

您上面的json无效。你应该删除,并在json的末尾添加]。 您可以在这里使用json validaor进行检查


您可以根据需要使用以下格式

{
    "records": [{
        "url": "http://i.imgur.com/1Pw3ehZ.jpg",
        "title": ["Sunset in Trinidad, CA",
            "bungholesex"
        ]
    }, {
        "url": "http://i.imgur.com/neQiFcf.jpg",
        "title": ["Humboldt Bay, Eureka California",
            "bungholesex"
        ]
    }, {
        "url": "http://imgur.com/dxKHGLV",
        "title": ["Crater Lake, Oregon in April",
            "CausticRain11"
        ]
    }, {
        "url": "http://imgur.com/RPv475F",
        "title": ["South Dakota is seriously beautiful.",
            "ratt1601"
        ]
    }, {
        "url": "http://imgur.com/oH1u7nk",
        "title": ["The view outside my back door. Virginia is alright, I guess...",
            "toowhitetofail"
        ]
    }]
}
请尝试以下代码:

$data = '[{"url": ["http://i.imgur.com/1Pw3ehZ.jpg"], "title": ["Sunset in Trinidad, CA", "bungholesex"]},
{"url": ["http://i.imgur.com/neQiFcf.jpg"], "title": ["Humboldt Bay, Eureka California", "bungholesex"]},
{"url": ["http://imgur.com/dxKHGLV"], "title": ["Crater Lake, Oregon in April", "CausticRain11"]},
{"url": ["http://imgur.com/RPv475F"], "title": ["South Dakota is seriously beautiful.", "ratt1601"]},
{"url": ["http://imgur.com/oH1u7nk"], "title": ["The view outside my back door. Virginia is alright, I guess...", "toowhitetofail"]}]';

$array = json_decode($data, true);
foreach ($array as &$item) {
    $item['url'] = $item['url'][0];
    $item['title'] = implode(', ', $item['title']);
}
echo json_encode($array);
我使用的是
url
的第一个数组项,因为在您的示例中它总是单个的。连接url是没有意义的-resut将不是有效的url。但如果您愿意,URL可以像
标题
一样连接起来

结果如下所示:

[
  {
    "url": "http://i.imgur.com/1Pw3ehZ.jpg",
    "title": "Sunset in Trinidad, CA, bungholesex"
  },
  {
    "url": "http://i.imgur.com/neQiFcf.jpg",
    "title": "Humboldt Bay, Eureka California, bungholesex"
  },
  {
    "url": "http://imgur.com/dxKHGLV",
    "title": "Crater Lake, Oregon in April, CausticRain11"
  },
  {
    "url": "http://imgur.com/RPv475F",
    "title": "South Dakota is seriously beautiful., ratt1601"
  },
  {
    "url": "http://imgur.com/oH1u7nk",
    "title": "The view outside my back door. Virginia is alright, I guess..., toowhitetofail"
  }
]

您可以检查它。

这只是代码的一部分。完整的json包含数千行。是否要将此json转换为其他json?而不是您想要的结果格式?不是另一种格式本身,更重要的是编辑此数据,以便名称中包含值而不是数组(如果有意义的话)。
{“url”:http://i.imgur.com/1Pw3ehZ.jpg“,”标题“:“加利福尼亚州特立尼达的日落,bungholesex”}
你想要这样的东西吗?数组中的所有项都连接成一个字符串。是的,这就是我想要得到的。我的最终目标是能够按名称解析数据,并将这些值用于各种事情,比如将它们输入mysql数据库。我不知道如何以我拥有的格式有效地提取数据。
[
  {
    "url": "http://i.imgur.com/1Pw3ehZ.jpg",
    "title": "Sunset in Trinidad, CA, bungholesex"
  },
  {
    "url": "http://i.imgur.com/neQiFcf.jpg",
    "title": "Humboldt Bay, Eureka California, bungholesex"
  },
  {
    "url": "http://imgur.com/dxKHGLV",
    "title": "Crater Lake, Oregon in April, CausticRain11"
  },
  {
    "url": "http://imgur.com/RPv475F",
    "title": "South Dakota is seriously beautiful., ratt1601"
  },
  {
    "url": "http://imgur.com/oH1u7nk",
    "title": "The view outside my back door. Virginia is alright, I guess..., toowhitetofail"
  }
]