note 38854 added to function.array-merge-recursive
| From: | t dot tom at succont dot de | Date: | Fri, 09 Jan 2004 01:09:28 +0000 |
| Subject: | note 38854 added to function.array-merge-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63181@lists.php.net to get a copy of this message | ||
Here my modification of shemari's Code for Replacing Values in an Array. My modification will
return the new Array, not handle it by reference.
Original Array will not be touched.
Hope it helps anyone. Most thanks goes to shemari ;o)
<?php
/**
* Merges two arrays and replace existing Entrys
*
* Merges two Array like the PHP Function array_merge_recursive.
* The main difference is that existing Keys will be replaced with new Values,
* not combined in a new Sub Array.
*
* Usage:
* $newArray = array_merge_replace( $array, $newValues );
*
* @access puplic
* @author Tobias Tom <t.tom@succont.de>
* @param array $array First Array with 'replaceable' Values
* @param array $newValues Array which will be merged into first one
* @return array Resulting Array from replacing Process
*/
function array_merge_replace( $array, $newValues ) {
foreach ( $newValues as $key => $value ) {
if ( is_array( $value ) ) {
if ( !isset( $array[ $key ] ) ) {
$array[ $key ] = array();
}
$array[ $key ] = $this->array_merge_replace( $array[ $key ], $value );
} else {
if ( isset( $array[ $key ] ) && is_array( $array[ $key ] ) ) {
$array[ $key ][ 0 ] = $value;
} else {
if ( isset( $array ) && !is_array( $array ) ) {
$temp = $array;
$array = array();
$array[0] = $temp;
}
$array[ $key ] = $value;
}
}
}
return $array;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38854
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38854&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38854&report=yes
Search -- http://master.php.net/manage/user-notes.php