note 27191 added to ref.array

From: Date: Tue, 26 Nov 2002 18:12:06 +0000
Subject: note 27191 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-40005@lists.php.net to get a copy of this message
Here's a function that will set an arbitary array field to an arbitary value: function array_field_to_value(&$array, $field, $value) { if ( is_array($field) ) { $head = array_shift($field); if ( count($field) == 0 ) { $array[$head] = $value; } else { array_field_to_value($array[$head], $field, $value); } } else { $array[$field] = $value; } } I find this useful for doing things like this: $post_arg = "1,10,pizza,orange,cat"; $post_val = "something"; $myarray = array(); array_field_to_value($myarray, explode(',', $post_arg), $post_val); Which gets the same results as doing: $myarray = array(); $myarray['1']['10']['pizza']['orange']['cat'] = "something"; -- http://www.php.net/manual/en/ref.array.php http://master.php.net/manage/user-notes.php?action=edit+27191 http://master.php.net/manage/user-notes.php?action=delete+27191 http://master.php.net/manage/user-notes.php?action=reject+27191

« previous php.notes (#40005) next »