note 100789 modified in function.array-key-exists by danbrown

From: Date: Sat, 06 Nov 2010 23:28:35 +0000
Subject: note 100789 modified in function.array-key-exists by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173818@lists.php.net to get a copy of this message
I created this function that uses array key exist to compare a form and a table to see if something has changed. This can be very helpfull if you need to update a table record from a form but you do not want to display all table fields. <?php function($data_from_db, $form_data) { $data = $data_from_db; $keys = array_keys($data); for($i = 0; $i < count($data); $i++) { if(!array_key_exists($keys[$i], $form_data)) { $dbobject->$keys[$i] = $data[$keys[$i]]; } else { $dbobject->$keys[$i] = $form_data[$keys[$i]]; } } return $dbobject; } ?> you can then use the dbobject to update the table. --was-- I created this function that uses array key exist to compare a form and a table to see if something has changed. This can be very helpfull if you need to update a table record from a form but you do not want to display all table fields. function($data_from_db, $form_data) { $data = $data_from_db; $keys = array_keys($data); for($i = 0; $i < count($data); $i++) { if(!array_key_exists($keys[$i], $form_data)) { $dbobject->$keys[$i] = $data[$keys[$i]]; } else { $dbobject->$keys[$i] = $form_data[$keys[$i]]; } } return $dbobject; } you can then use the dbobject to update the table. http://php.net/manual/en/function.array-key-exists.php

« previous php.notes (#173818) next »