0

Something has changed on the JSON string i'm getting from a hosted site. the color element is now returning false if empty and my script is not catching it - I'm getting undefined error for $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 Answer 1

1

It has nothing to do with JSON. The notice complains about lines

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

$color .= $value['name'] is a shortcut syntax for $color = $color . $value['name'];

On the first iteration $color is undefined, so you get the notice.

You need to initialise these variables to get rid of the notices:

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) {
    ......
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks that did the trick. only question is why did this error just present itself? I have been using this code for 6-8 months without an error? the only change I have made was to add an SSL certificate to the site???
Notices are usually disabled in production environment. Apparently you enabled them. Either error_reporting() in the code, or error_reporting = in php.ini php.net/manual/en/…. If you see these notices on the page, rather than in logs, there are chances that display_errors = in php.ini has been changed from 'off' to 'on'.
yep, thats it, I did turn on error_reporting() - Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.