-1

The custom field of one of the plugins contains an array of data. I get this data, but I can't figure out how to make it a readable view for output on the page.

Such data is recorded in the field:

{"1 filename.doc":{"name":"filename.doc","url":"https://example.com/filename.doc","file":"/var/www/example.com/filename.doc","type":"application/msword","size":50688}}

From this I need a name and a link to do something like this:

<a href="https://example.com/filename.doc">filename.doc</a>

I am just learning and I will be grateful for any hint!

hobopol
  • 1
  • 1

1 Answers1

0

Well you have a json string so the first step would be to convert it to a PHP array using json_decode:

$jsonArray = json_decode($jsonString, true);

The json strucutre looks like it can describe mutlitple files so we will run a foreach to iterate over the entries in the json. Within the foreach we will create an html link using the data from the json:

foreach($jsonArray as $file=>$fileAttributes) {
    echo '<a href="'.$fileAttributes['url'].'">'.$fileAttributes['name'].'</a>';
}

Here's a working example: https://3v4l.org/5TAWd

cOle2
  • 4,725
  • 1
  • 24
  • 26