note 94458 added to function.array-replace
| From: | tufan dot oezduman at googlemail dot com | Date: | Fri, 06 Nov 2009 12:19:58 +0000 |
| Subject: | note 94458 added to function.array-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162441@lists.php.net to get a copy of this message | ||
a little enhancement to dyer85 at gmail dot com's function below:
<?php
if (!function_exists('array_replace'))
{
function array_replace( array &$array, array &$array1, $filterEmpty=false )
{
$args = func_get_args();
$count = func_num_args()-1;
for ($i = 0; $i < $count; ++$i) {
if (is_array($args[$i])) {
foreach ($args[$i] as $key => $val) {
if ($filterEmpty && empty($val)) continue;
$array[$key] = $val;
}
}
else {
trigger_error(
__FUNCTION__ . '(): Argument #' . ($i+1) . ' is not an array',
E_USER_WARNING
);
return NULL;
}
}
return $array;
}
}
?>
this will allow you to "tetris-like" merge arrays:
<?php
$a= array(
0 => "foo",
1 => "",
2 => "baz"
);
$b= array(
0 => "",
1 => "bar",
2 => ""
);
print_r(array_replace($a,$b, true));
?>
results in:
Array
(
[0] => foo
[1] => bar
[2] => baz
)
----
Server IP: 217.160.72.57
Probable Submitter: 88.79.188.214
----
Manual Page -- http://www.php.net/manual/en/function.array-replace.php
Edit -- https://master.php.net/note/edit/94458
Del: integrated -- https://master.php.net/note/delete/94458/integrated
Del: useless -- https://master.php.net/note/delete/94458/useless
Del: bad code -- https://master.php.net/note/delete/94458/bad+code
Del: spam -- https://master.php.net/note/delete/94458/spam
Del: non-english -- https://master.php.net/note/delete/94458/non-english
Del: in docs -- https://master.php.net/note/delete/94458/in+docs
Del: other reasons-- https://master.php.net/note/delete/94458
Reject -- https://master.php.net/note/reject/94458
Search -- https://master.php.net/manage/user-notes.php