multidim-array-bijwerken
Gesponsorde koppelingen
PHP script bestanden
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
function array_update( &$p_aOrigArray, $p_aValues, $p_bDelete = false )
{
if ( is_array( $p_aOrigArray ) && is_array( $p_aValues ) ) {
foreach ($p_aValues as $k => $v) {
if ( is_array( $p_aValues[$k] ) && is_array( $p_aOrigArray[$k] ) ) {
/**
* If the sub element is an array, run the update cycle through each of its elements.
* call_user_func_array is used for its reference compatibility (passing a reference
* with a normal function call will result in a warning).
*/
call_user_func_array( __FUNCTION__, array( &$p_aOrigArray[$k], $p_aValues[$k], $p_bDelete ) );
} elseif ( $p_bDelete ) {
/**
* Delete the element if asked for.
*/
unset( $p_aOrigArray[$k] );
} else {
/**
* Or just update with the new value.
*/
$p_aOrigArray[$k] = $p_aValues[$k];
}
}
}
}
?>
function array_update( &$p_aOrigArray, $p_aValues, $p_bDelete = false )
{
if ( is_array( $p_aOrigArray ) && is_array( $p_aValues ) ) {
foreach ($p_aValues as $k => $v) {
if ( is_array( $p_aValues[$k] ) && is_array( $p_aOrigArray[$k] ) ) {
/**
* If the sub element is an array, run the update cycle through each of its elements.
* call_user_func_array is used for its reference compatibility (passing a reference
* with a normal function call will result in a warning).
*/
call_user_func_array( __FUNCTION__, array( &$p_aOrigArray[$k], $p_aValues[$k], $p_bDelete ) );
} elseif ( $p_bDelete ) {
/**
* Delete the element if asked for.
*/
unset( $p_aOrigArray[$k] );
} else {
/**
* Or just update with the new value.
*/
$p_aOrigArray[$k] = $p_aValues[$k];
}
}
}
}
?>
Gebruik is als volgt:
Code (php)
1
2
3
4
2
3
4
<?php
array_update( $aOrigineleArray, $aArrayMetNieuweWaarden, $bOfDeKeysVerwijderdMoetenWorden );
print_r( $aOrigineleArray );
?>
array_update( $aOrigineleArray, $aArrayMetNieuweWaarden, $bOfDeKeysVerwijderdMoetenWorden );
print_r( $aOrigineleArray );
?>
De eerste parameter (met de originele array) wordt dus meegegeven als reference.
Als je de keys wilt verwijderen maakt het niet uit wat de nieuwe waarden zijn in de tweede parameter. Zet dan de derde parameter op true.