Laravel,将JSON数组转换为数组,并且仅从数组中获取一个对象


Laravel, convert JSON array to Array and only get one object from the Array

<?php
namespace App'Http'Controllers;
use Illuminate'Http'Request;
use Vinelab'Http'Client as HttpClient;
use App'Requests'SearchRequest;
use App'Http'Requests;
use App'Http'Controllers'Controller;
class SearchResults extends Controller
{
    public function index()
    {
        return view('results.search-results');
    }
    public function store(Requests'SearchRequest $request)
    {
        $search_phrase = $request->input('search');
        $client = new HttpClient;
        $response = $client->get('https://www.reddit.com/search.json?q='. $search_phrase .'');
        $responseArray = $response->json();
        dd($responseArray);
        return view('results.search-results');
    }
}  

使用上面的代码,我使用此HTTP服务调用reddit API

https://github.com/Vinelab/http/tree/master

返回的响应给了我一个包含大量数据的数组,但我只想从中获取标题字段并将其解析为一个 Laravel数组,该数组可以发送到一个视图,我将在其中显示标题在 foreach 循环中。

我想也许可以将结果的标题存储在数据库中,然后查询数据库并将其发送到视图。我是所有这些的新手,所以任何帮助和理论将不胜感激。

在 Laravel 5.2 中是否有办法将此 JSON 数组的输出转换为可以紧凑并发送到视图的可用数组?

您可以这样做,将 json 转换为数组格式。

json_decode($response->content(), true);

并可以通过此访问

$response[0]['title']