note 79526 deleted from function.key by googleguy

From: Date: Tue, 22 Oct 2013 06:42:56 +0000
Subject: note 79526 deleted from function.key by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196609@lists.php.net to get a copy of this message
Note Submitter: egingell at sisna dot com ---- What's the point of lines 14 and 25? $array is destroyed when the function returns. If you want this to work as written, change line 2 to this: (pass $array by reference instead of by value) <? function add_key ($value, &$array) // returns the key of the value added or existing already within the array ?> > danny at dannymendel dot com > 12-Apr-2006 09:41 > An auto incremental key value function, returning the key value: > > 1 <? > 2 function add_key ($value, $array) // returns the key of the value added or existing > already within the array > 3 { > 4 if (is_array ($array)) > 5 { > 6 if (!in_array ($value, $array)) > 7 { > 8 ksort ($array); > 9 >10 end ($array); >11 >12 $key = key ($array) + 1; >13 >14 $array[$key] = $value; >15 } >16 else >17 { >18 $key = array_search ($value, $array); >19 } >20 } >21 else >22 { >23 $key = 1; >24 >25 $array[$key] = $value; >26 } >27 return $key; >28 } >29 ?>

« previous php.notes (#196609) next »