Search code examples
phparrayssimple-html-dom

How to access this kind of array value?


I scraped Twitter media with simple_html_dom and got this array result:

Array
(
    [0] => https://pbs.twimg.com/media/DWyUfBdVwAE9bmJ.jpg
    [1] => https://pbs.twimg.com/media/DWyUgBUVMAASB_g.jpg
    [2] => https://pbs.twimg.com/media/DWyUg-xU0AEHdyL.jpg
)
Array
(
    [0] => https://pbs.twimg.com/media/CYba_z2UwAAoSuC.jpg
)
Array
(
    [0] => https://pbs.twimg.com/media/CTyJ52dUEAAirWw.jpg
    [1] => https://pbs.twimg.com/media/CTyJ5yMUkAAbvZq.jpg
    [2] => https://pbs.twimg.com/media/CTyJ5k4VEAAC4f7.jpg
    [3] => https://pbs.twimg.com/media/CTyJ5f4VAAAZ08g.jpg
)
Array
(
    [0] => https://pbs.twimg.com/media/CTyJJyeUcAAo8kt.jpg
    [1] => https://pbs.twimg.com/media/CTyJJ2KUkAIKOPa.jpg
    [2] => https://pbs.twimg.com/media/CTyJJycUkAAL29J.jpg
)

How can I access every $value[0] for each array?

this is my code based on https://github.com/alexroan/twitter-scraper :

    <?php
  require 'TwitterScraper.php';
    $feed = TwitterScraper::get_feed('maretaso_u');
    foreach($feed as $tweet){ 

        $media = $tweet->media;
        echo "<pre>";
        print_r($media);   
        echo "<pre>";

    }

Solution

  • I had to make some guesses based on the info you gave. But this is what I did:

    <?php
    
    $feed = TwitterScraper::get_feed('maretaso_u');
    
    $filtered = array_map(function($tweet) {
        return $tweet->media[0];
    }, $feed);
    
    var_dump($filtered);