note 27551 added to language.types.array
| From: | wjs at sympaticoDOTca dot php dot net | Date: | Tue, 10 Dec 2002 18:45:07 +0000 |
| Subject: | note 27551 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40671@lists.php.net to get a copy of this message | ||
<?php
/*
Here is an array difficultly to watch for.
The numeric indices into an array are NOT sequential, they are associative
keys.
*/
// define an array with an offset of one
$AnArray = array (1 => "Dog", "Cat", "Bat");
// add an element to the 0 index
$AnArray[0] = "Rat";
// print this
print_r($AnArray);
// get this output
/*
Array ( [1] => Dog [2] => Cat [3] => Bat [0] => Rat )
*/
/*
The new value was added at the end, not the beginning as intended.
Try using the push/pop/shift/unshift functions if order is
important in the array.
*/
// This would have the proper effect from the example.
// array_unshift($AnArray,"Rat");
/*
The index will not unshift to less then 0 so additional unshift calls will
begin to change the indices of the remaining elements.
/*
Or use the ksort() function to sort the keys.
*/
?>
--
http://www.php.net/manual/en/language.types.array.php
http://master.php.net/manage/user-notes.php?action=edit+27551
http://master.php.net/manage/user-notes.php?action=delete+27551
http://master.php.net/manage/user-notes.php?action=reject+27551