note 92220 modified in function.array-replace-recursive by danbrown
| From: | danbrown@php.net | Date: | Tue, 14 Jul 2009 22:14:58 +0000 |
| Subject: | note 92220 modified in function.array-replace-recursive by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158304@lists.php.net to get a copy of this message | ||
This might help out people who don't have 5.3 running:
<?php
function array_join($original)
{
// Get array arguments
$arrays = func_get_args();
// Loop through arrays
foreach ($arrays as $array)
{
// Loop through array key/value pairs
foreach ($array as $key => $value)
{
// Value is an array
if (is_array($value))
{
// Traverse the array; replace or add result to original array
$original[$key] = array_join($array[$key], $original[$key]);
}
// Value is not an array
else
{
// Replace or add current value to original array
$original[$key] = $value;
}
}
}
// Return the joined array
return $original;
}
// Example
$first = array('one' => '1', 'two' => '2',
'three' => '3');
$second = array('one' => 'one', 'two' => '2',
'three' => 'three', 'four' => '4');
print_r(array_join($first, $second));
?>
Output:
Array
(
[one] => one
[two] => 2
[three] => three
[four] => 4
)
--was--
This might help out people who don't have 5.3 running:
<?
function array_join($original)
{
// Get array arguments
$arrays = func_get_args();
// Loop through arrays
foreach ($arrays as $array)
{
// Loop through array key/value pairs
foreach ($array as $key => $value)
{
// Value is an array
if (is_array($value))
{
// Traverse the array; replace or add result to original array
$original[$key] = array_join($array[$key], $original[$key]);
}
// Value is not an array
else
{
// Replace or add current value to original array
$original[$key] = $value;
}
}
}
// Return the joined array
return $original;
}
// Example
$first = array('one' => '1', 'two' => '2',
'three' => '3');
$second = array('one' => 'one', 'two' => '2',
'three' => 'three', 'four' => '4');
print_r(array_join($first, $second));
?>
Output:
Array
(
[one] => one
[two] => 2
[three] => three
[four] => 4
)
http://php.net/manual/en/function.array-replace-recursive.php