Get specific content in a JSON

0

I need to capture the uid and the osgjsSize that are within Files . I do not know how to solve this error ( I am a student / novice ). Thank you very much in advance for your help.

<?php $url = 'https://pastebin.com/raw/ZivzYNvM';
$data = file_get_contents($url);
$json = json_decode($data, true);
foreach ($json as $uid) {
echo "<td>".$uid['files'][0]['uid']."</td>";
}
?>
    
asked by Santiago Tapia'z 07.11.2018 в 03:45
source

1 answer

1

The pastebin source file that you sample has only one entry that contains a files key, whose content is:

"files": [

    {

        "uid": "6a525a5d9ec1452086427929bf0890ae",
        "flag": 0,
        "osgjsSize": 59831,
        "wireframeSize": 72052,
        "modelSize": 161452,
        "osgjsUrl": "https://media.sketchfab.com/urls/418eacc1eefe446591aff5738b79d595/dist/models/6a525a5d9ec1452086427929bf0890ae/file.osgjs.gz"

    }

],

In the absence of more information, it gives the impression that what you get when doing:

$json = json_decode($data, true);

It is an associative array where house key is of the type

/i/models/<uid>[/<otro string]

These keys have as value an associative array where one of its elements can be files

If the files element exists, then apparently it has the form of an array of one or more associative arrays with the keys you need, uid and osgsize .

Since your pastebin has only one element, and the idea is to give a general solution, let's think instead that the origin of your file is link , which has several entries with the files property, and even one of them contains two files.

My approach would be:

First: you get the complete associative array

$url = 'https://pastebin.com/raw/dhU1Q97J'; // ojo que este es mi pastebin, no el original
$data      = file_get_contents($url);
$json      = json_decode($data, true);

Filter those entries that have a files

element
$filtered_array = array_filter($json, function ($element) {
    return isset($element['files']);
});

Of these, you're only interested in the key files and not the rest of the properties, so you map the filtering to stay only with files

$mapped_array = array_map(function ($element) {
    return $element['files'];
}, $filtered_array);

At this point $mapped_array is an array of 0 to N elements, each of which is the files element of one of the entries in your original array, and therefore is an array of 0 to N files .

That arrangement you can print it as:

foreach ($mapped_array as $files) {
        foreach ($files as $file) {
            echo '<tr><td>' . $file['uid'] . '</td><td>' . $file['osgjsSize'] . '</td></tr>';
        }

    }

To make it look a little prettier, I wrapped everything in a check to print the table only if there are files to show:

<?php
$url  = 'https://pastebin.com/raw/dhU1Q97J';
$data = file_get_contents($url);
$json = json_decode($data, true);

$filtered_array = array_filter($json, function ($element) {
    return isset($element['files']);
});

$mapped_array = array_map(function ($element) {
    return $element['files'];
}, $filtered_array);

if (sizeof($mapped_array) > 0) {
    echo '<table>';
    echo '<tr><th>Uid</th><th>&nbsp;&nbsp;&nbsp;&nbsp;osgjsSize</th></tr>';

    foreach ($mapped_array as $files) {
        foreach ($files as $file) {
            echo '<tr><td>' . $file['uid'] . '</td><td style="text-align:right">' . $file['osgjsSize'] . '</td></tr>';
        }

    }
    echo '</table>';
}

The result you can see here:

link

    
answered by 07.11.2018 / 07:35
source