[英]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 我正在尝试访问每个属性中的title属性
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. 我想将它们解析为可以发送到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来实现像这样的嵌套数组
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 ++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.