Level 122
You have the right idea, but this line
if ($arr['SchoolPhoneNumber']) {
checks if the SchoolPhoneNumber is empty - not if it does not exist in the array. Try;
if (array_key_exists('SchoolPhoneNumber',$arr)) {
dd($array['SchoolInfo']);
outputs:
array:2 [▼
0 => array:17 [▼
"@attributes" => array:2 [▶]
"URN" => "123891"
"LAId" => "933"
"EstablishmentId" => "4455"
"SchoolName" => "Preston School"
"SchoolFullName" => "Preston School"
"SchoolURL" => "http://www.preston.somerset.sch.uk"
"OperationalStatus" => "2"
"GenderOfEntry" => "C"
"Boarders" => "No"
"Special" => "No"
"SchoolAddress" => array:10 [▶]
"HeadTeacherInfo" => array:2 [▶]
"Phase" => "SY"
"NCYearGroupList" => array:1 [▶]
"SIF_ExtendedElements" => array:1 [▶]
"SIF_Metadata" => array:2 [▶]
]
1 => array:18 [▼
"@attributes" => array:2 [▶]
"URN" => "136894"
"LAId" => "933"
"EstablishmentId" => "4455"
"SchoolName" => "Preston School Academy"
"SchoolFullName" => "Preston School Academy"
"SchoolURL" => "http://www.prestonschool.co.uk/"
"SchoolPhoneNumber" => array:1 [▶]
"OperationalStatus" => "1"
"GenderOfEntry" => "C"
"Boarders" => "No"
"Special" => "No"
"SchoolAddress" => array:10 [▶]
"HeadTeacherInfo" => array:2 [▶]
"Phase" => "SY"
"NCYearGroupList" => array:1 [▶]
"SIF_ExtendedElements" => array:1 [▶]
"SIF_Metadata" => array:2 [▶]
]
]
when I try and output it in my loop:
I get: Undefined index: SchoolPhoneNumber
here is my loop:
foreach ($array['SchoolInfo'] as $arr) {
//dd($arr);
if ($arr['SchoolPhoneNumber']) {
$schoolphone = $arr['SchoolPhoneNumber']['Number'];
}
$schoolstreet = $arr['SchoolAddress']['Street'];
echo 'URL:' . $arr['SchoolURL'];
if ($schoolphone != '') {
echo 'Phone: ' . $schoolphone . '<br />';
}
echo 'street: ' . $schoolstreet . '<br />';
}
You have the right idea, but this line
if ($arr['SchoolPhoneNumber']) {
checks if the SchoolPhoneNumber is empty - not if it does not exist in the array. Try;
if (array_key_exists('SchoolPhoneNumber',$arr)) {
Please or to participate in this conversation.