Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 错误的输出-数组(1){[“说明”]=>;字符串(0)";}_Php_Arrays_Json - Fatal编程技术网

Php 错误的输出-数组(1){[“说明”]=>;字符串(0)";}

Php 错误的输出-数组(1){[“说明”]=>;字符串(0)";},php,arrays,json,Php,Arrays,Json,如何获得此脚本的良好输出: 源JSON: 我需要将值放入数组,并在下一步中放入变量。 值为结果->数据->选项卡->统一内容->项目(如果标题=标题,如果文本=说明,则为类型->文本) 脚本: function getContent($data) { $tabs = $data->result->data->tab; foreach($tabs as $tab){ $content = []; if(isset($tab->

如何获得此脚本的良好输出: 源JSON:

我需要将值放入数组,并在下一步中放入变量。 值为结果->数据->选项卡->统一内容->项目(如果标题=标题,如果文本=说明,则为类型->文本)

脚本:

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);
}

现在是输出:数组(1){[“描述”]=>string(0)”}

请下次明确您要寻找的是什么(什么是“好的输出”?)

使用json_解码($json_文件,true)。 即:



同样,这可能不是您要寻找的答案,因为它不清楚您在OP中要寻找的是什么。

请下次明确您要寻找的是什么(什么是“良好输出”?)

使用json_解码($json_文件,true)。 即:



同样,这可能不是您要寻找的答案,因为它不清楚您在OP中要寻找的是什么。

您需要在
foreach
循环之前初始化
$content
,然后返回它。否则,您只返回存在
unified\u content->item
的第一个选项卡中的内容。该选项卡仅包含一项,没有标题和空文本:

    "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
        }]
    }]
如果没有
标题
对象,您还应该在内部循环之前初始化
$arr
,而不是在
if

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;
}

您需要在
foreach
循环之前初始化
$content
,然后返回它。否则,您只返回存在
unified\u content->item
的第一个选项卡中的内容。该选项卡仅包含一项,没有标题和空文本:

    "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
        }]
    }]
如果没有
标题
对象,您还应该在内部循环之前初始化
$arr
,而不是在
if

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;
}

一个好的输出应该是什么样的?另外,输入应该是什么样的?我需要在数组中获取值。在
foreach()
循环中返回的内容可能重复,因此您只需要返回第一个选项卡中的内容,其中包含统一的内容。这就是你想要的吗?一个好的输出是什么样子的?还有,输入是什么样子的?我需要在数组中获取值。在
foreach()
循环中返回的值可能重复,因此你只需要返回第一个选项卡中的内容,并包含统一的内容。这就是你想要的吗?因为他没有从
foreach
循环中得到错误,他一定已经解码了JSON。天哪,这是真的,我的错!因为他没有从
foreach
循环中得到错误,所以他一定已经解码了JSON!从你的函数我得到这个输出:Array(),抱歉,没有把它移得足够远。从你的函数我得到这个输出:Array(),抱歉,没有把它移得足够远。