Home > Back-end >  how to access the name or url content from this array in php?
how to access the name or url content from this array in php?

Time:07-25

How can I show this data inside an HTML table in PHP?

I convert the JSON to an array using the "json_decode" function.

Just wanted to know a method to convert the array to an HTML table or fetch specific details such as name or URL.

Thanks

[
  {
    "trends": [
      {
        "name": "#GiftAGamer",
        "url": "http://twitter.com/search?q=#GiftAGamer",
        "promoted_content": null,
        "query": "#GiftAGamer",
        "tweet_volume": null
      },
      {
        "name": "#TransDayOfRemembrance",
        "url": "http://twitter.com/search?q=#TransDayOfRemembrance",
        "promoted_content": null,
        "query": "#TransDayOfRemembrance",
        "tweet_volume": 45852
      },
      {
        "name": "Mourão",
        "url": "http://twitter.com/search?q=Mourão",
        "promoted_content": null,
        "query": "Mourão",
        "tweet_volume": 12614
      },
      {
        "name": "Taysom Hill",
        "url": "http://twitter.com/search?q="Taysom Hill"",
        "promoted_content": null,
        "query": ""Taysom Hill"",
        "tweet_volume": 20311
      },
      {
        "name": "Geraldo",
        "url": "http://twitter.com/search?q=Geraldo",
        "promoted_content": null,
        "query": "Geraldo",
        "tweet_volume": 30166
      }
    ],
    "as_of": "2020-11-20T19:37:52Z",
    "created_at": "2020-11-19T14:15:43Z",
    "locations": [
      {
        "name": "Worldwide",
        "woeid": 1
      }
    ]
  }
]

CodePudding user response:

You can use json_decode and then specify the index you are going to use. Use something like this.

  $json = '[
      {
        "trends": [
          {
            "name": "#GiftAGamer",
            "url": "http://twitter.com/search?q=#GiftAGamer",
            "promoted_content": null,
            "query": "#GiftAGamer",
            "tweet_volume": null
          },
          {
            "name": "#TransDayOfRemembrance",
            "url": "http://twitter.com/search?q=#TransDayOfRemembrance",
            "promoted_content": null,
            "query": "#TransDayOfRemembrance",
            "tweet_volume": 45852
          },
          {
            "name": "Mourão",
            "url": "http://twitter.com/search?q=Mourão",
            "promoted_content": null,
            "query": "Mourão",
            "tweet_volume": 12614
          },
          {
            "name": "Taysom Hill",
            "url": "http://twitter.com/search?q="Taysom Hill"",
            "promoted_content": null,
            "query": ""Taysom Hill"",
            "tweet_volume": 20311
          },
          {
            "name": "Geraldo",
            "url": "http://twitter.com/search?q=Geraldo",
            "promoted_content": null,
            "query": "Geraldo",
            "tweet_volume": 30166
          }
        ],
        "as_of": "2020-11-20T19:37:52Z",
        "created_at": "2020-11-19T14:15:43Z",
        "locations": [
          {
            "name": "Worldwide",
            "woeid": 1
          }
        ]
      }
    ]';
    
    $json= json_decode($json);
    $newJson=$json[0]->trends;
    foreach($newJson as $n)
    {
       echo $n->name.'<br>';
       echo $n->url.'<br>';
    }

CodePudding user response:

First you want to understand the structure. The JSON you pasted is an array with exactly one (1) element, which is an object with the keys trends, as_of, created_at and locations.

In PHP you can access an element in an array via the index (starting at 0) $array[index], eg. $array[0].

An object on the other hand is accessible via the method syntax, which in PHP in a single arrow ->, eg. $object->attribute.

So, in order to get the name of the first element in "trends", you have to access each array and/or object from the start.

<?php
$json = json_decode($string); // json string you posted
$object = $json[0]; // get the first element of the overall array
$trends = $object->trends; // get the trends attribute of the first object, which is an array again
$first_trend = $trends[0]; // array can be accessed via the index again.
$first_name = $first_trend->name; // a trend is an object again with attributes name, url, promoted_content, etc.

Of course you want to access this data in an dynamic way, which is where loops come in handy. PHP offers foreach, which can iterate over array elements and also object attributes. Take a read: https://www.php.net/manual/en/language.oop5.iterations.php
https://www.php.net/manual/en/control-structures.foreach.php

With this, you can iterate all trends like this

foreach($trends as $trend) {
  echo $trend->name;
}

You of course can wrap HTML (eg. a table) around.

<?php
echo "<table>";
$json = json_decode($string);
$trends = $json[0]->trends;
foreach($trends as $trend) {
  echo "<tr>"; // new table row

  foreach($trend as $key => $value) {
    // this iterates over all attributes of trend
    echo "<td>" . $value . "</td>";
  }

  echo "</tr>"; // close table row
}
echo "</table>";
?>

This is an example of a loop inside another loop. The outer loop iterates all trends, the inner loop iterates the attributes of the current trend.

  • Related