简体   繁体   中英

Get the values from a nested JSON array in PHP Laravel

So I have a piece of code that I have been fighting with for a while now.

Here is the code that I have

<?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);
    }
}

And this returns a nested array that looks like this

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

I am trying to access the title attribute that is inside each of these

  1 => array:2 [▶]
  2 => array:2 [▶]
  3 => array:2 [▶]
  4 => array:2 [▶]
  5 => array:2 [▶]
  6 => array:2 [▶]
  7 => array:2 [▶]

I want to parse them to an array that I can send to a Laravel View.

Every time I try to acccess this I get undefined index or offset and I am at a loss as to how to go about this. Can anybody assist me in finding a solution to this problem?

Edit ---------------------

I am now using this and it is working perfectly

$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 ++;
        }

Basically you would need three foreach in your case to achieve the nested arrays like this

foreach($SearchResultsArray as $ThreeLevelArray){

foreach($ThreeLevelArray as $TwoLevelArray) {

foreach($TwoLevelArray as $OneevelArray) {

//your here son :)

}
}

}
$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 ++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM