note 58321 deleted from function.array-push by peehaa
| From: | peehaa@php.net | Date: | Tue, 21 Mar 2017 08:38:25 +0000 |
| Subject: | note 58321 deleted from function.array-push by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208919@lists.php.net to get a copy of this message | ||
Note Submitter: ludvig dot ericson at gmail dot com
----
Previous comment was not fully imitating the array_push behaviour,
1) does not return number of items pushed
2) can only handle one array to push
> Revised associative_push function with absolute reference of arg1 array; left unchanged if arg2
> is empty.
<?php
// Append associative array elements
function array_push_associative(&$arr) {
$args = func_get_args();
array_unshift($args); // remove &$arr argument
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key => $value) {
$arr[$key] = $value;
$ret++;
}
}
}
return $ret;
}
$theArray = array();
echo array_push_associative($theArray, $items, $moreitems) . ' items added to $theArray.';
?>