【问题标题】:Get the values from a nested JSON array in PHP Laravel从 PHP Laravel 中的嵌套 JSON 数组中获取值
【发布时间】:2016-06-06 23:09:50
【问题描述】:

所以我有一段代码我一直在争论一段时间。

这是我的代码

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

我正在尝试访问其中的每个标题属性

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

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

每次我尝试访问它时,我都会得到未定义的索引或偏移量,我不知道如何去做。 任何人都可以帮助我找到解决这个问题的方法吗?

编辑 ---------

我现在正在使用它,它运行良好

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

【问题讨论】:

  • 你基本上需要3个foreach才能访问

标签: php arrays json laravel-5.2 reddit


【解决方案1】:

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

foreach($SearchResultsArray as $ThreeLevelArray){

foreach($ThreeLevelArray as $TwoLevelArray) {

foreach($TwoLevelArray as $OneevelArray) {

//your here son :)

}
}

}

【讨论】:

  • 一旦我到达 //your here son :) 我如何访问数组中的“标题”?会打赌 $OneevelArray->title;
  • 你刚刚到达 0 => array() 1 => array() 2=>array() 所以你也想进入那个数组?
  • 是的,在每个里面 1 => array:2 [▶] 2 => array:2 [▶] 3 => array:2 [▶] 4 => array:2 [▶] 5 => 数组:2 [▶] 6 => 数组:2 [▶] 7 => 数组:2 [▶] 有我需要的数据
  • 再循环一次 :) 所以如果你想要 key 然后你可以使用 foreach($OneLevelArray as $key => $value) { echo $key die();} 检查它是否有效跨度>
  • 然后是 $OneevelArray->title?,让我快速尝试一下
【解决方案2】:
$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 ++;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 2021-05-31
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多