未定义的变量,但它在数组中定义

时间:2019-05-02 12:33:06

标签: php

为什么我的代码说$ ​​publishRequest是未定义的。它确实是在数组中定义的

这只是我的代码的一部分,但这部分是问题隐藏的部分

已经在数组上方尝试过:

php

$publishRequest = array()

if($status1=="up") {
    $publishRequest = array(
        "instance_id" => INSTANCE_ID,
        "name" => INSTANCE_NAME,
        "group" => INSTANCE_GROUP,
        "description" => "Running on Controller.Ligowave.com",
        "contents" => array(
            array(
                "name" => "Status",
                "contents" => array(
                    array(
                        "icon" => "information",
                        "type" => "label",
                        "title" => $name1,
                        "subtitle" => "Uptime is: " . $uptimetime1
                    )
                )
            )
        )
    );
} else {
   array(
       "icon" => "error",
       "type" => "label",
        "title" => $name1,
        "subtitle" => "Down since: " . $lastseendate1);
}

try {
    echo "<p>Publish</p>";
    $publishResponse = request(options, $publishRequest);
    echo "<p>".$publishResponse."</p></br></br>";
}

1 个答案:

答案 0 :(得分:2)

如果$status1不是== 'up',则可以在ELSE中创建一个数组,但不要将其设置为变量$publishRequest

else {
   $publishRequest = array(
                        "icon" => "error",
                        "type" => "label",
                        "title" => $name1,
                        "subtitle" => "Down since: " . $lastseendate1);
}