Replace array value in multidimensional array - basic Q
my question seems pretty basic, but i can't get around it. I try to replace the second value in a multidimensional array with another array, which happens, but i loose it in my foreach loop. Everything is just an example. So failures like missing quotes happend in here. The original is a function inside a class. Thanks!
This is how the config array looks like:
$config_arr = array(
'foo_a' => array( 'foo_a_singular', 'foo_a_plural', array('assign_me_aa', 'assign_me_ab') );
'foo_b' => array( 'foo_b_singular', 'foo_b_plural', 'assign_me_b')
);
This how the processing happens:
function process_foo( $config_arr ) {
foreach ( $config_arr as $config_data ) {
$replacement_data = array( 'bar_me', 'bar_her', 'bar_some' );
$config_data[1] = $replacement_data;
var_dump($config_data);
}
print_r( $config_arr );
}
The v开发者_JAVA百科ar_dump
shows me that i replaced/added the data, but the print_r
tells me that nothing happend.
I'll brush up on this answer in a minute, but either the scope is local for config_data or you can only read and not write. I've had this problem before. you have to use the actual config_arr in the loop, this can be accomplished with a counter variable. someone else might know a better/different way to do this.
Point is that I tried to use a numerical index $config_data[1]
for a associative array: 'foo_b' => array( 'foo_b_singular', 'foo_b_plural', 'assign_me_b')
, which will never work. You have to use stuff like key()
or array_keys()
.
精彩评论