从PHP Laravel中的嵌套JSON数组中获取值

时间:2016-02-24 15:17:17

标签: php arrays json laravel-5.2 reddit

所以我有一段代码,我一直在与之斗争一段时间。

这是我的代码

<?php

namespace App\Http\Controllers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

use App\Requests\SearchRequest;
use Vinelab\Http\Client as HttpClient;

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)
    {

        $searchPhrase = $request->input('search');

        $client = new HttpClient;

        $response = $client->get('https://www.reddit.com/search.json?q='. urldecode($searchPhrase) .'');

        $response = collect($response->json());

        $responseDecode = json_decode($response, true);

        $SearchResultsArray = $responseDecode;

        dd($SearchResultsArray);
    }
}

这将返回一个看起来像这样的嵌套数组

 array:2 [▼
  "kind" => "Listing"
  "data" => array:5 [▼
    "facets" => []
    "modhash" => ""
    "children" => array:25 [▼
      0 => array:2 [▼
        "kind" => "t3"
        "data" => array:52 [▶]
      ]
      1 => array:2 [▶]
      2 => array:2 [▶]
      3 => array:2 [▶]
      4 => array:2 [▶]
      5 => array:2 [▶]
      6 => array:2 [▶]

    ]
    "after" => "t3_38lgh9"
    "before" => null
  ]
]

我正在尝试访问每个

中的title属性
  1 => array:2 [▶]
  2 => array:2 [▶]
  3 => array:2 [▶]
  4 => array:2 [▶]
  5 => array:2 [▶]
  6 => array:2 [▶]
  7 => array:2 [▶]

我想将它们解析为一个可以发送到Laravel View的数组。

每当我试图接受这个时,我都会得到未定义的索引或偏移量,而我对如何解决这个问题感到很茫然。 任何人都可以协助我找到解决这个问题的方法吗?

编辑---------------------

我现在正在使用它并且它工作正常

$allData=[];

        $counter = 1; 
        foreach ($posts as $post) {
            //foreach post get the data and store it in a database
            $allData[$counter]['title']= $post['data']['title'];
            $sentiment = SentimentAnalysis::decision($allData[$counter]['title']);
            $allData[$counter]['created']= $post['data']['created'];

            RedditPosts::create([
                    'title' => $allData[$counter]['title'], 
                    'created' => date($allData[$counter]['created']),
                    'sentiment' => $sentiment,
                    'search_identifier' => $search_id,
                    'search_phrase' => $searchPhrase
                    ]);

            $counter ++;
        }

2 个答案:

答案 0 :(得分:0)

基本上你需要三个foreach来实现像这样的嵌套数组

foreach($SearchResultsArray as $ThreeLevelArray){

foreach($ThreeLevelArray as $TwoLevelArray) {

foreach($TwoLevelArray as $OneevelArray) {

//your here son :)

}
}

}

答案 1 :(得分:0)

$allData=[];

    $counter = 1; 
    foreach ($posts as $post) {
        //foreach post get the data and store it in a database
        $allData[$counter]['title']= $post['data']['title'];
        $sentiment = SentimentAnalysis::decision($allData[$counter]['title']);
        $allData[$counter]['created']= $post['data']['created'];

        RedditPosts::create([
                'title' => $allData[$counter]['title'], 
                'created' => date($allData[$counter]['created']),
                'sentiment' => $sentiment,
                'search_identifier' => $search_id,
                'search_phrase' => $searchPhrase
                ]);

        $counter ++;
    }