Home > Enterprise >  Check value nested array
Check value nested array

Time:05-04

I've been struggling with getting info from an array for some time now.

I have an array structure like this:

Array
(
    [0] => Array
        (
            [voornaam] => Leonie
            [naam] => xxxxxx
            [gebruikersnaam] => xxxxxxxxxxxxxxxxxxxxx
            [internnummer] => 12469
            [status] => actief
            [extravoornamen] => 
            [initialen] => 
            [roepnaam] => 
            [geslacht] => f
            [geboortedatum] => xxxx-10-xxxx
            [geboorteplaats] => xxxxxx
            [geboorteland] => België
            [rijksregisternummer] => 
            [straat] => xxxxxxxxx
            [huisnummer] => 71
            [busnummer] => 
            [postcode] => xxxx
            [woonplaats] => xxxxxxxxxxxxx
            [land] => België
            [emailadres] => xxxxxxxxxxxxxxxxxxx
            [website] => 
            [mobielnummer] => 
            [telefoonnummer] => 
            [fax] => 
            [instantmessenger] => 
            [sorteerveld] => dxxxxxxxx-leonie
            [stamboeknummer] => 1700024
            [koppelingsveldschoolagenda] => 
            [basisrol] => 1
            [klasnummer] => 6
            [referenceIdentifier] => 5235_400_0
            [scannableCode] => 34ff3123-bf41-5f67-8a37-cdf53226183a
            [isEmailVerified] => 1
            [isAuthenticatorAppEnabled] => 
            [isYubikeyEnabled] => 
            [groups] => Array
                (
                    [0] => Array
                        (
                            [id] => 8
                            [code] => officlass_pwd5af1aad026164
                            [name] => 5HW
                            [desc] => 5HW
                            [isKlas] => 1
                            [isOfficial] => 1
                        )

                    [1] => Array
                        (
                            [id] => 1906
                            [code] => 
                            [name] => MOD SPO
                            [desc] => module Sport
                            [isKlas] => 1
                            [isOfficial] => 
                        )

                )

            [advies_2] => 
            [1F/1L] => 
            [graad_(20-21)] => 2
            [jaar_(20-21)] => 4
            [DIFF periode 1] => 
            [DIFF periode 2] => 
            [DIFF periode 3] => 
            [DIFF periode 4] => 
            [DIFF periode 5] => 
            [Thuis eten] => 
            [graad 2021_2022] => 3
            [module] => MOD SPO
        )

My goal is to extract the 'name' from '[groups] => Array' but only when '[isOfficial] => 1'

$smartschool = new SmartschoolConnection;

$sGroupName = "Leerlingen";

$leerlingen = $smartschool->getAllAccountsExtended($sGroupName);

foreach ($leerlingen as $leerling)
{

            $voornaam           =       $leerling['voornaam'];
            $achternaam         =       ucwords(strtolower($leerling['naam']));
            $gebruikersnaam     =       $leerling['gebruikersnaam'];
            $geslacht           =       $leerling['geslacht'];
            $datestring         =       date_create($leerling['geboortedatum']);
            $geboortedatum      =       date_format($datestring, 'd-m-Y');
            $internnummer       =       $leerling['internnummer'];
            $stamboeknummer     =       $leerling['stamboeknummer'];
            $klasnummer         =       $leerling['klasnummer'];
            $refid              =       $leerling['referenceIdentifier'];

 } 

I've been experimenting a lot but can't figure it out, I thought I could solve it with the following:

    foreach ($leerlingen as $leerling) {
        //  Check type
        if (is_array($leerling)){
            //  Scan through inner loop
            foreach ($leerling as $klas) {

                if($klas['isOfficial'] == "1") {

                    $klas = $klas['name'];

                }

            }
        } 
        else
        {

            $voornaam           =       $leerling['voornaam'];
            $achternaam         =       ucwords(strtolower($leerling['naam']));
            $gebruikersnaam     =       $leerling['gebruikersnaam'];
            $geslacht           =       $leerling['geslacht'];
            $datestring         =       date_create($leerling['geboortedatum']);
            $geboortedatum      =       date_format($datestring, 'd-m-Y');
            $internnummer       =       $leerling['internnummer'];
            $stamboeknummer     =       $leerling['stamboeknummer'];
            $klasnummer         =       $leerling['klasnummer'];
            $refid              =       $leerling['referenceIdentifier'];

        }
    } 

But it trows this error:

PHP Fatal error:  Uncaught TypeError: Cannot access offset of type string on string in /home/nginx/domains/***********/public/xacokiosk/api/classusers.php:17

Can someone help me with this?

Thank you in advance

CodePudding user response:

You are using the wrong value in foreach. Try this:

foreach ($leerlingen as $leerling) {
    //  Check type
    if (is_array($leerling['groups'])){
        //  Scan through inner loop
        foreach ($leerling['groups'] as $klas) {

            if($klas['isOfficial'] == "1") {

                $group = $klas['name'];
                break;
                /*
                If you are going to accept more than one item, change the code above for this one:
                $groups[] = $klas['name'];
                */

            }

        }
    } 

    $voornaam           =       $leerling['voornaam'];
    $achternaam         =       ucwords(strtolower($leerling['naam']));
    $gebruikersnaam     =       $leerling['gebruikersnaam'];
    $geslacht           =       $leerling['geslacht'];
    $datestring         =       date_create($leerling['geboortedatum']);
    $geboortedatum      =       date_format($datestring, 'd-m-Y');
    $internnummer       =       $leerling['internnummer'];
    $stamboeknummer     =       $leerling['stamboeknummer'];
    $klasnummer         =       $leerling['klasnummer'];
    $refid              =       $leerling['referenceIdentifier'];

} 
  • Related