note 45141 deleted from function.array-keys by aidan

From: Date: Fri, 27 Aug 2004 15:13:10 +0000
Subject: note 45141 deleted from function.array-keys by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75426@lists.php.net to get a copy of this message
Note Submitter: glennh at webadept dot net ---- One way i've used this function is binding arrays to objects from queries. such as : <? function User($uid) { $uq = "select * from customers where uid = '$uid'"; $ur = db_exec($uq); $ua = db_fetch_assoc($ur); $mykeys = array_keys($ua); $len = count($mykeys); for($i=0;$i < $len;$i++) { $this->$mykeys[$i] = $ua[$mykeys[$i]]; } } // end function ?> of course, it could be better to do this from the other angle as well. <? function bindHashToObject( $hash, &$obj, $prefix=NULL, $checkSlashes=true, $bindAll=false ) { is_array( $hash ) or die( "bindHashToObject : hash expected" ); is_object( $obj ) or die( "bindHashToObject : object expected" ); if ($bindAll) { foreach ($hash as $k => $v) { $obj->$k = ($checkSlashes && get_magic_quotes_gpc()) ? stripslashes( $hash[$k] ) : $hash[$k]; } } else if ($prefix) { foreach (get_object_vars($obj) as $k => $v) { if (isset($hash[$prefix . $k ])) { $obj->$k = ($checkSlashes && get_magic_quotes_gpc()) ? stripslashes( $hash[$k] ) : $hash[$k]; } } } else { foreach (get_object_vars($obj) as $k => $v) { if (isset($hash[$k])) { $obj->$k = ($checkSlashes && get_magic_quotes_gpc()) ? stripslashes( $hash[$k] ) : $hash[$k]; }// end if }// end foreach } // end if else //echo "obj="; print_r($obj); exit; } // end function ?> I'm not really sure which is the better method, but using the array_keys function has always worked rather well for me, your milage may varry.

« previous php.notes (#75426) next »