note 49984 deleted from function.array-push by peehaa
| From: | peehaa@php.net | Date: | Tue, 21 Mar 2017 08:37:12 +0000 |
| Subject: | note 49984 deleted from function.array-push by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208914@lists.php.net to get a copy of this message | ||
Note Submitter: josh at digitalfruition dot com
----
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.