note 49984 added to function.array-push
| From: | josh at digitalfruition dot com | Date: | Mon, 14 Feb 2005 18:17:29 +0000 |
| Subject: | note 49984 added to function.array-push | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85024@lists.php.net to get a copy of this message | ||
Note that array_push() will, as described, return the COUNT of the array after adding a new item,
not necessarily the INDEX of that new item:
<?php
$array = array(3 => 'three', 5 => 'five');
echo "\$array = ";
print_r($array);
echo "\n\n";
$to_push = array(1,2,4,);
foreach($to_push as $var)
{
echo "calling array_push(\$array,$var); retval is ";
echo array_push($array,$var);
echo "\n";
}
echo "\$array = ";
print_r($array);
?>
The output of above is:
$array = Array
(
[3] => three
[5] => five
)
calling array_push($array,1); retval is 4
calling array_push($array,2); retval is 5
calling array_push($array,4); retval is 6
$array = Array
(
[3] => three
[5] => five
[7] => seven
[8] => 1
[9] => 2
[10] => 4
)
Notice how when array_push($array,1) was called, the new element has a key of 8 but array_push()
returns 4.
----
Manual Page -- http://www.php.net/manual/en/function.array-push.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49984
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49984&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49984&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49984&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49984&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49984&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49984&report=yes
Search -- http://master.php.net/manage/user-notes.php