NYCPHP Meetup

NYPHP.org

[nycphp-talk] array_get_value?

Kenneth Downs ken at secdat.com
Fri Sep 21 11:45:25 EDT 2007


I wrote my own as one of my very first PHP library functions.  It's 
extremely useful for writing simple and clean code, like:

$state = ArrayValue($_POST,'state','NY');

Then you need one in javascript:

function getProperty(object,property,defvalue) {
    defvalue = defvalue==null ? '' : defvalue;
    if(object == null) return defvalue
    if(typeof(object[property])=='undefined') return defvalue;
    else return object[property];
}

Michael B Allen wrote:
> Hey,
>
> I find myself doing a lot of:
>
>   if (isset($data['foo']))
>       // use $data['foo']
>
> Is there an existing array function that gets the array value or
> returns a default value like:
>
>   function array_get_value($arr, $key, $def) {
>       return array_key_exists($arr, $key) ? $arr[$key] : $def;
>   }
>
> ?
>
> Mike
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>   


-- 
Kenneth Downs
Secure Data Software, Inc.
www.secdat.com    www.andromeda-project.org
631-689-7200   Fax: 631-689-0527
cell: 631-379-0010




More information about the talk mailing list