Saving data from a nested JSON decoded array into a Laravel DB
<p>I have an array that outputs like this</p>

<pre><code>array:25 [▼
      1 =&gt; array:3 [▼
        "title" =&gt; "The Superfish problem is Microsoft's opportunity to fix a huge problem and have manufacturers ship their computers with a vanilla version of Windows. Versions of windows preloaded with crapware (and now malware) shouldn't even be a thing."
        "created" =&gt; 1424600986
        "comments" =&gt; array:6 [▼
          0 =&gt; "&amp;lt;!-- SC_OFF --&amp;gt;&amp;lt;div class="md"&amp;gt;&amp;lt;p&amp;gt;Lenovo did a stupid/terrible thing by loading their computers with malware. But HP and Dell have been loading their computers with unnecessary software for years now.&amp;lt;/p&amp;gt;"
          1 =&gt; ""
          2 =&gt; "&amp;lt;p&amp;gt;The people that aren&amp;amp;#39;t smart enough to uninstall that software, are also not smart enough to blame Lenovo or HP instead of Microsoft (and honestly, Microsoft deserves some of the blame for allowing these OEM installs anways).&amp;lt;/p&amp;gt;"
          3 =&gt; ""
          4 =&gt; "&amp;lt;p&amp;gt;There are many other complications that result from all these differentiated versions of Windows. The time is ripe for Microsoft to stop letting companies ruin windows before the consumer even turns the computer on.&amp;lt;/p&amp;gt;"
          5 =&gt; "&amp;lt;/div&amp;gt;&amp;lt;!-- SC_ON --&amp;gt;"
        ]
      ]
      2 =&gt; array:3 [▶]
      3 =&gt; array:3 [▶]
</code></pre>

<p>What I want to do is to get to the comments array and the for each comment inside the nested array I want to extract each one and save it into the DB</p>

<p>I have tried to do it like this </p>

<pre><code>$counter = 1; 
foreach ($posts as $post) {
    //foreach post get the data and store it in a database
    $allData[$counter]['title']= $post['data']['title'];

    //Foreach Title, get a sentiment using the sentiment analysis
    $sentiment = SentimentAnalysis::decision($allData[$counter]['title']);

    $allData[$counter]['created']= $post['data']['created'];

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

    foreach($posts['comments'] as $comment) {
        $sentiment = SentimentAnalysis::decision($allData[$counter]['comments']);
              $comment = RedditComment::create([
                'comment' =&gt; $comment,
                'sentiment' =&gt; $sentiment,
                'search_identifier' =&gt; $search_id,
                'search_phrase' =&gt; $searchPhrase
                ]);
           }

    $counter ++;
}   
</code></pre>

<p>However this does not work. I get undefined index on comments. I believe that I am on the right track, I just need to find a way to access this array of comments within the array of posts.</p>

<p>Is there a way to loop through this array and grab every comment strip the html and then save it into the DB </p>

<p>***************************SOLVED*************************************</p>

<p>This was solved like this</p>

<pre><code>//Collect the data to be stored in the DB
    $allData=[];

    $counter = 1; 


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

        //Foreach Title, get a sentiment using the sentiment analysis
        $sentiment = SentimentAnalysis::decision($allData[$counter]['title']);

        $allData[$counter]['created']= $post['data']['created'];

        //store this in the DB to have access to the information at a later stage
        RedditPosts::create([
                'title' =&gt; $allData[$counter]['title'], 
                'created' =&gt; date($allData[$counter]['created']),
                'sentiment' =&gt; $sentiment,
                'search_identifier' =&gt; $search_id,
                'search_phrase' =&gt; $searchPhrase
                ]);

        $allData[$counter]['comments'] = explode(PHP_EOL, $post['data']['selftext_html']);

        foreach($allData[$counter]['comments'] as $comment) {

            $sentiment = SentimentAnalysis::decision($comment);

                  //store this in the DB to have access to the information at a later stage
                  RedditComments::create([
                    'comment' =&gt; strip_tags(html_entity_decode($comment)),
                    'created' =&gt; date($allData[$counter]['created']),
                    'sentiment' =&gt; $sentiment,
                    'search_identifier' =&gt; $search_id,
                    'search_phrase' =&gt; $searchPhrase
                    ]);
               }

        $counter ++;
    }   
</code></pre>