dongxuanchao1425 2017-11-13 16:21
浏览 48
已采纳

输出错误 - 数组(1){[“描述”] =>字符串(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) "" }

  • 写回答

2条回答 默认 最新

  • donglanying3855 2017-11-13 17:02
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭