boyjarv's avatar

I would like to get the SchoolPhoneNumber['Number'] from the second array in my foreach

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 />';
                    }
0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
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)) {
Snapey's avatar

You can also use the null coalesce operator;

replace

    if ($arr['SchoolPhoneNumber']) {
        $schoolphone = $arr['SchoolPhoneNumber']['Number'];
     }

with just

        $schoolphone = $arr['SchoolPhoneNumber']['Number'] ?? '';
Braunson's avatar

There's a few ways you can do this. I've included a few examples below with comments

$array = [
    [
        "@attributes" =>  [],
        "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" => [],
        "HeadTeacherInfo" => [],
        "Phase" => "SY",
        "NCYearGroupList" => [],
        "SIF_ExtendedElements" => [],
        "SIF_Metadata" => [],
    ],
    [
        "@attributes" => [],
        "URN" => "136894",
        "LAId" => "933",
        "EstablishmentId" => "4455",
        "SchoolName" => "Preston School Academy",
        "SchoolFullName" => "Preston School Academy",
        "SchoolURL" => "http://www.prestonschool.co.uk/",
        "SchoolPhoneNumber" => ['number' => '555-555-5555'],
        "OperationalStatus" => "1",
        "GenderOfEntry" => "C",
        "Boarders" => "No",
        "Special" => "No",
        "SchoolAddress" => [],
        "HeadTeacherInfo" => [],
        "Phase" => "SY",
        "NCYearGroupList" => [],
        "SIF_ExtendedElements" => [],
        "SIF_Metadata" => [],
    ]
];

// Assuming the keys are not set
foreach ($array as $key => $arr) {
    // Remember it starts at 0
    if ($key == 1) {
        // Access $arr['SchoolAddress']
    }
}

// Tracking the key (assuming keys are set)
$i = 0;
foreach ($array as $arr) {
    if ($i == 1) {
        // Access $arr['SchoolAddress']
    }
    $i++;
}


// Assuming you wanted to check if it's there at all for any item in the array
foreach ($array as $arr) {
    if (isset($arr['SchoolArrress']) {
        // Do things
    }
}

Also you can use array_key_exists in lieu of isset.

Please or to participate in this conversation.