0

how can i get good output of this script: Source JSON: https://ufile.io/sn0hu

I need get values into array and in the next step take into variables. Values is result->data->tab->unified_content->item (type->text if header = title and if text = description)

Script:

function getContent($data) {
    $tabs = $data->result->data->tab;
    foreach($tabs as $tab){
        $content = [];
        if(isset($tab->unified_content->item)) {
            foreach($tab->unified_content->item as $item) {
                if($item->type->name == 'header') {
                    $arr = [];
                    $arr['title'] = $item->text;
                } else {
                    $arr['description'] = $item->text;
                    $content[] = $arr;
                }                   
            }
            return $content;
        }   
    }
}

$test = getContent($data);
foreach ($test as $lol) {
    var_dump($lol);
}

Now is output this: array(1) { ["description"]=> string(0) "" }

David
  • 57
  • 8
  • 2
    What would a good output look like? – Don't Panic Nov 13 '17 at 16:21
  • Also, what does the input look like? – Don't Panic Nov 13 '17 at 16:23
  • I need get values inside of arrays. – David Nov 13 '17 at 16:26
  • Possible duplicate of [How to convert JSON string to array](https://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array) – William Perron Nov 13 '17 at 16:36
  • You're returning inside the `foreach()` loop, so you're going to return just the contents from the first tab with unified content. Is that what you want? – Barmar Nov 13 '17 at 16:52
  • I need get values into array and in the next step take into variables. Values is result->data->tab->unified_content->item (type->text if header = title and if text = description). – David Nov 13 '17 at 16:54
  • But do you need to get the values from *all* the tabs with unified content, or just the first one? – Barmar Nov 13 '17 at 16:55
  • Yes from all, but i must filter it to right values and then assign them.If i insert into the function instead of return print_r($content) i got right values, but why i didnt get it out of function ? – David Nov 13 '17 at 16:58

2 Answers2

2

You need to initialize $content before the foreach loop, and return it after. Otherwise, you're only returning the contents from the first tab where unified_content->item exists. That tab contains just one item, with no header and empty text.:

    "item": [{
        "id": 17229,
        "text": "",
        "align": "l",
        "type": {
            "id": 3,
            "name": "image"
        },
        "width": "l",
        "shape": "s",
        "swajp": 0,
        "icon": 273,
        "margin": 1,
        "image": [{
            "id": 10482,
            "src": "image.php?id=18432&app_id=5786&key=a1bd33754a582bc1094e5f1b170adbb4747b7bdb",
            "title": "V ned\u011bli 12.11. od 14:00 hrajeme v Bra\u0161kov\u011b",
            "width": 1920,
            "height": 1345
        }, {
            "id": 10480,
            "src": "image.php?id=18430&app_id=5786&key=62d75ca8ea4c16fdd6a4bec8c48848f692b843bf",
            "title": "Dom\u00e1c\u00ed prohra na penalty s Jedom\u011blicemi 2:3 (1:2) Pen: 2:4",
            "width": 1920,
            "height": 1218
        }, {
            "id": 10483,
            "src": "image.php?id=18433&app_id=5786&key=1e1479dcf8de55f57e29e1a16f428f4414c76032",
            "title": "Unho\u0161\u0165 spadla na 11. m\u00edsto v tabulce",
            "width": 1920,
            "height": 1280
        }, {
            "id": 49465,
            "src": "image.php?id=90502&app_id=5786&key=3103a185854b771cdad784950d343bb3e143bec6",
            "title": "\u00dasp\u011b\u0161n\u00fd fotbalov\u00fd kemp",
            "width": 1000,
            "height": 750
        }]
    }]

You should also initialize $arr before the inner loop, not in the if, in case there's no header object.

function getContent($data) {
    $tabs = $data->result->data->tab;
    $content = [];
    foreach($tabs as $tab){
        if(isset($tab->unified_content->item)) {
            $arr = [];
            foreach($tab->unified_content->item as $item) {
                if($item->type->name == 'header') {
                    $arr['title'] = $item->text;
                } else {
                    $arr['description'] = $item->text;
                    $content[] = $arr;
                }                   
            }
        }   
    }
    return $content;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Please next time make it clear what it is you're looking for (what is a "good output"?).

Use json_decode($json_file ,true). Ie:

<?php

$json_file = file_get_contents("testing.json");
$json_decode = json_decode($json_file, true); //Sets it as an array

print_r($json_decode['result']['status']); //Will print out "done" in this case

?>

Again, this might not be the answer you're looking for since it wasn't exactly clear what it was you're looking for in the OP.

RAZERZ
  • 200
  • 1
  • 11