note 28552 deleted from function.array-shift by sniper
| From: | sniper@php.net | Date: | Mon, 03 Feb 2003 16:16:48 +0000 |
| Subject: | note 28552 deleted from function.array-shift by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-43335@lists.php.net to get a copy of this message | ||
Here are some solutions on how to get not only the value but also the key of the 1st element of an
associative array.
- foreach ($myarray as $key => $value)
{
// use here $key, and $value as you want
unset ($myarray[$key]); // if you want to get rid of the 1st element, as array_shift does
break;
}
- $temporaryarray = array_keys ($myarray);
$key = $temporaryarray[0];
$value = $myarray[$key];
unset ($myarray[$key]); // if you want to get rid of the 1st element
or
$value = array_shift ($myarray); // if you want to get rid of the 1st element
- reset; // to be sure
list ($key,$value) = each ($myarray);
unset ($myarray[$key]); // if you want to get rid of the 1st element, as array_shift does
Mathieu BOIS