JSON字符串包含false而不是空或null

时间:2017-03-08 16:25:04

标签: php json

我从托管网站获取的JSON字符串发生了变化。 color元素现在返回false如果为空且我的脚本没有捕获它 - 我为$color $time $emailbody

获取了未定义的错误

JSON:

[{"name":"Bowie Hospital Center","time":["","",""],"color":[false,false,false]},
{"name":"Calvert Memorial Hospital","time":["","","",""],"color":[false,false,false,false]},
{"name":"Charles Regional (UM)","time":["","","",""],"color":[false,false,false,false]},
{"name":"Doctors Community Hospital","time":["","","",""],"color":[false,false,false,false]},
{"name":"Fort Washington Hospital","time":["","","",""],"color":[false,false,false,false]},
{"name":"Germantown EC","time":["","",""],"color":[false,false,false]},{"name":"Holy Cross Germantown","time":["","","",""],"color":[false,false,false,false]},
{"name":"Holy Cross Hospital","time":["","","",""],"color":[false,false,false,false]},
{"name":"Laurel Regional Medical Center","time":["","","",""],"color":[false,false,false,false]},
{"name":"Malcolm Grow","time":["","",""],"color":[false,false,false]},{"name":"Montgomery Medical Center (MedStar)","time":["","","",""],"color":[false,false,false,false]},
{"name":"Prince Georges Hospital Center","time":["","","","",""],"color":[false,false,false,false,false]},
{"name":"Shady Grove Advent Hospital","time":["","","",""],"color":[false,false,false,false]},
{"name":"Southern Maryland Hospital\u00a0 (MedStar)","time":["","","",""],"color":[false,false,false,false]},
{"name":"St. Mary\u2019s Hospital\u00a0 (MedStar)","time":["","","",""],"color":[false,false,false,false]},
{"name":"Suburban Hospital (JHM)","time":["","","","",""],"color":[false,false,false,false,false]},
{"name":"Walter Reed NMMC","time":["","","","",""],"color":[false,false,false,false,false]},
{"name":"Washington Adventist Hospital","time":["","","",""],"color":[false,false,false,false]},
{"name":"Childrens National Medical Center","time":["","","","",""],"color":[false,false,false,false,false]},
{"name":"George Washington Hospital","time":["","","","",""],"color":[false,false,false,false,false]},
{"name":"Georgetown University (MedStar)","time":["11:03","11:03","",""],"color":["background-color:#ffff00;color:#000000;","background-color:#ff0000;color:#000000;",false,false]},
{"name":"Howard University Hospital","time":["","","","",""],"color":[false,false,false,false,false]},
{"name":"MedStar Washington Hospital Center","time":["11:03","","","",""],"color":["background-color:#ffff00;color:#000000;",false,false,false,false]},
{"name":"Providence Hospital","time":["","","",""],"color":[false,false,false,false]},
{"name":"Sibley Memorial Hospital (JHM)","time":["","","",""],"color":[false,false,false,false]},
{"name":"United Medical Center","time":["","","",""],"color":[false,false,false,false]}]

PHP:

function myArray($arry) {
        $selected = array(5,6,7,10,12,15,17,18,22,24);
        foreach($arry as $key => $value) {
            if (in_array($key, $selected) || empty($selected)) {
                $color .= $value['name']. chr(10);
                $time .= $value['name']. chr(10);
                $emailbody .= $value['name']. '<br>';
                $length = count($value["color"]);

                // TEST of color alert
                print($color + '<br>' + chr(10) + '<br>');
                print($time + '<br>');
                print($emailbody + '<br>');
                print($length + '<br>');
                // end

                for ($i = 0; $i < $length; $i++) {
                    if($value['time'][$i] != "") {      

                        if(preg_match('/#ff0000/', $value['color'][$i])) {
                            $value['color'][$i] = '<font color="#ff0000">RED</font>';
                        }
                        if(preg_match('/#ffff00/', $value['color'][$i])) {
                            $value['color'][$i] = '<font color="#ffff00">YELLOW</font>';
                        }
                        if(preg_match('/#006600/', $value['color'][$i])) {
                            $value['color'][$i] = '<font color="#006600">GREEN (Mini Disaster)</font>';
                        }
                        if(preg_match('/#9933cc/', $value['color'][$i])) {
                            $value['color'][$i] = '<font color="#9933cc">PURPLE (Trauma ByPass)</font>';
                        }
                        if(preg_match('/#ff6600/', $value['color'][$i])) {
                            $value['color'][$i] = '<font color="#ff6600">ORANGE (ReRoute)</font>';
                        }

                        $time .= '  <b>' . $value["color"][$i] . '</b> - <i>' . $value["time"][$i] . '</i>' . chr(10); 
                        $color .= '  <b>' . $value["color"][$i] . '</b>' . chr(10); 
                        $emailbody .= '  <b>' . $value["color"][$i] . '</b> - <i>' . $value["time"][$i] . '</i><br>';

                    }
                }
            }
        }
        return array($time, $color, $emailbody);
    }

ERROR:

Notice: Undefined variable: color in /index.php on line 5
Notice: Undefined variable: time in /index.php on line 6
Notice: Undefined variable: emailbody in /index.php on line 7

1 个答案:

答案 0 :(得分:1)

它与JSON无关。通知抱怨行

            $color .= $value['name']. chr(10);
            $time .= $value['name']. chr(10);
            $emailbody .= $value['name']. '<br>';

$color .= $value['name']$color = $color . $value['name'];

的快捷语法

在第一次迭代$color未定义时,您会收到通知。

您需要初始化这些变量以删除通知:

function myArray($arry) {
    $selected = array(5,6,7,10,12,15,17,18,22,24);
    $color = $time = $emailbody = ""; // <== initialising with empty strings
    foreach($arry as $key => $value) {
    ......