note 79526 added to function.key
| From: | egingell at sisna dot com | Date: | Sun, 02 Dec 2007 11:12:17 +0000 |
| Subject: | note 79526 added to function.key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132729@lists.php.net to get a copy of this message | ||
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 ?>
----
Server IP: 209.41.74.194
Probable Submitter: 207.224.195.213
----
Manual Page -- http://www.php.net/manual/en/function.key.php
Edit -- https://master.php.net/note/edit/79526
Del: integrated -- https://master.php.net/note/delete/79526/integrated
Del: useless -- https://master.php.net/note/delete/79526/useless
Del: bad code -- https://master.php.net/note/delete/79526/bad+code
Del: spam -- https://master.php.net/note/delete/79526/spam
Del: non-english -- https://master.php.net/note/delete/79526/non-english
Del: in docs -- https://master.php.net/note/delete/79526/in+docs
Del: other reasons-- https://master.php.net/note/delete/79526
Reject -- https://master.php.net/note/reject/79526
Search -- https://master.php.net/manage/user-notes.php