0

I have a script that outputs some json. My json looks like: I am trying to get the twitter trends and displaying to my website.

Array
(
    [0] => Array
        (
            [trends] => Array
                (
                    [0] => Array
                        (
                            [name] => #Allah_In_Quran
                            [url] => http://twitter.com/search?q=%23Allah_In_Quran
                            [promoted_content] => 
                            [query] => %23Allah_In_Quran
                            [tweet_volume] => 216174
                        )

                    [1] => Array
                        (
                            [name] => Last Prophet Saint Rampal Ji
                            [url] => http://twitter.com/search?q=%22Last+Prophet+Saint+Rampal+Ji%22
                            [promoted_content] => 
                            [query] => %22Last+Prophet+Saint+Rampal+Ji%22
                            [tweet_volume] => 161376
                        )

                    [2] => Array
                        (
                            [name] => BBB3 SHOOT WRAP UP
                            [url] => http://twitter.com/search?q=%22BBB3+SHOOT+WRAP+UP%22
                            [promoted_content] => 
                            [query] => %22BBB3+SHOOT+WRAP+UP%22
                            [tweet_volume] => 224067
                        )

                    [3] => Array
                        (
                            [name] => PROUD OF LOUIS
                            [url] => http://twitter.com/search?q=%22PROUD+OF+LOUIS%22
                            [promoted_content] => 
                            [query] => %22PROUD+OF+LOUIS%22
                            [tweet_volume] => 336126
                        )

                    [4] => Array
                        (
                            [name] => #MaiBhiTiranga
                            [url] => http://twitter.com/search?q=%23MaiBhiTiranga
                            [promoted_content] => 
                            [query] => %23MaiBhiTiranga
                            [tweet_volume] => 28770
                        )

                    [5] => Array
                        (
                            [name] => #BURARS
                            [url] => http://twitter.com/search?q=%23BURARS
                            [promoted_content] => 
                            [query] => %23BURARS
                            [tweet_volume] => 36755
                        )

                    [6] => Array
                        (
                            [name] => LOUIS TOMLINSON
                            [url] => http://twitter.com/search?q=%22LOUIS+TOMLINSON%22
                            [promoted_content] => 
                            [query] => %22LOUIS+TOMLINSON%22
                            [tweet_volume] => 342661
                        )

                    [7] => Array
                        (
                            [name] => Rudrakaal Tomorrow
                            [url] => http://twitter.com/search?q=%22Rudrakaal+Tomorrow%22
                            [promoted_content] => 
                            [query] => %22Rudrakaal+Tomorrow%22
                            [tweet_volume] => 
                        )

                    [8] => Array
                        (
                            [name] => #TeamIndia
                            [url] => http://twitter.com/search?q=%23TeamIndia
                            [promoted_content] => 
                            [query] => %23TeamIndia
                            [tweet_volume] => 60971
                        )

                    [9] => Array
                        (
                            [name] => #SSMBPrideOfTollywood
                            [url] => http://twitter.com/search?q=%23SSMBPrideOfTollywood
                            [promoted_content] => 
                            [query] => %23SSMBPrideOfTollywood
                            [tweet_volume] => 72219
                        )

                    [10] => Array
                        (
                            [name] => Xhaka
                            [url] => http://twitter.com/search?q=Xhaka
                            [promoted_content] => 
                            [query] => Xhaka
                            [tweet_volume] => 45763
                        )

                    
            [as_of] => 2021-03-06T15:20:54Z
            [created_at] => 2021-03-06T03:44:12Z
            [locations] => Array
                (
                    [0] => Array
                        (
                            [name] => India
                            [woeid] => 23424848
                        )

                )

        )

)

I have using the below code getting the output

$getfield = '?id=23424848';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo "<pre>";
print_r($string);
echo "</pre>";

I have tried with foreach loop, but I can't able to fetch the data from jason. I want to convert this jason data to table format.

  • What kind of table format are you looking for? – Beshambher Chaukhwan Mar 06 '21 at 16:54
  • I am looking for table format "Rank", "Twitter trending Topic", "Tweet Volume". These 3 data's available in json output. – Balamurugan Mar 06 '21 at 17:15
  • Can you give the json data as string before json encode – Beshambher Chaukhwan Mar 06 '21 at 17:17
  • I have tried but not working. Here Json array data's are showing correctly. We have to convert this data to HTML table or Datatable to show the data' table format in front end. – Balamurugan Mar 06 '21 at 17:38
  • You write "*My json looks like*" and showing us an array. While you have JSON data decoded already to arry, that's easy to iterate it and generate the HTML markup for table. – biesior Mar 06 '21 at 17:42
  • Yes, My JSON data already decoded to array. I am displaying the array results. Please have a look my output. thanks. – Balamurugan Mar 06 '21 at 17:45
  • next use `foreach($string[0]['trends'] as $item){ ... }` and prepare HTML you want in the loop, that's really basics. If you want us to write your code feel free to paypal somebody. – biesior Mar 06 '21 at 17:49
  • can we really get some $'s in paypal for simple loop writings like this? xD – Beshambher Chaukhwan Mar 06 '21 at 17:52
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Syscall Mar 06 '21 at 19:17

1 Answers1

0

Try the below loop:

echo "<pre><table>";
echo "<tr><th>name</th><th>url</th></tr>";
foreach ($datas as $data) {
    $trends = $data['trends'];
    foreach ($trends as $tweet) {
        echo "<tr>";
        echo "<td>".$tweet['name']."</td>";
        echo "<td>".$tweet['url']."</td>";
        echo "</tr>";
    }
}
echo "</table></pre>";

Provided that json decoding is working fine. Demo code in here: https://ideone.com/NfHmiJ

Beshambher Chaukhwan
  • 1,418
  • 2
  • 9
  • 13