Web lists-archives.com

Re: [PHP] Basic php code question




On Tue, Jan 30, 2018 at 3:07 PM, Dave <dealtek@xxxxxxxxx> wrote:

> I could use a bit of help making sense of this inherited code...
>
> $_premises = array(
> 'APARTMENTS'=>'APARTMENT','APT',
> 'APTS'=>'APT',
> 'ROOMS'=>'RM','RMS','RM',
> '#'=>'APT#','APT #','SUITE#','SUITE #','OFFICE#','OFFICE #','Suite');
>
>
> Q: when i see something like:
>
> 'APARTMENTS'=>'APARTMENT','APT',
>
> does that mean...
>
> 'APARTMENTS' can equal/match 'APARTMENT' ?and/or match? can equal 'APT'
>
> or what does this mean?
>

No, not without writing code to do the mapping. This array mixes integer
and string keys which PHP allows but causes confusion. Reformatting gives

    $_premises = array(
        'APARTMENTS' => 'APARTMENT',
        'APT',
        'APTS' => 'APT',
        'ROOMS' => 'RM',
        'RMS',
        'RM',
        '#' => 'APT#',
        'APT #',
        'SUITE#',
        'SUITE #',
        'OFFICE#',
        'OFFICE #',
        'Suite',
    );

The cases without a => will receive integer keys starting from zero.

    0 => 'APT',
    1 => 'RMS',
    etc.

Likely it's used to validate/map a premise to a normalized form like this:

    if (!in_array($premise, $_premises)) {   // valid premise?
        if (isset($_premises[$premise]) {       // no, maps to a valid
premise?
            $premis = $_premises[$premise];   // yes, map it
        }
        else {
            throw new InvalidArgumentException("Premise $premise is
invalid");
        }
    }
    // $premise is valid...

Cheers!
David