[nycphp-talk] Accessing non-existent array elements ok?
Michael B Allen
ioplex at gmail.com
Mon Aug 18 11:26:21 EDT 2008
On Mon, Aug 18, 2008 at 8:51 AM, Kenneth Downs <ken at secdat.com> wrote:
> Michael B Allen wrote:
>>
>> I seem to recall getting a warning trying to access array elements
>> that did not exist but I can't seem to trigger that issue right now.
>>
>> The following code:
>>
>> <?php
>> $a = array('a' => 'b');
>> $result = $a['c'];
>> var_dump($result);
>>
>> yields the following:
>>
>> $ php -f t.php
>>
>
> Michael, I found the following routine to be very useful dealing with
> arrays:
>
> function a(&$array, $key,$default='') {
> if (isset($array[$key])) return $array[$key];
> else return $default;
> }
>
> This allows you to code up simpler unconditional routines with code like:
>
> $value = a($myArray,$key,'AccetableDefault');
That's pretty much what I'm doing right now:
function _array_val($a, $k, $d = null) {
if (is_array($a) && array_key_exists($k, $a))
return $a[$k];
if (func_num_args() == 3)
return $d;
throw new Exception("No such array key: $k");
}
I was just hoping to streamline things further.
Thanks,
Mike
--
Michael B Allen
PHP Active Directory SPNEGO SSO
http://www.ioplex.com/
More information about the talk
mailing list