Reputation: 1185
It may just be that I've checked out already for the weekend, but having a bit of trouble updating an associative array based on a certain value. For example, here is what I have so far:
$slideshow_vars = array(
'js_animation' => $slideshow_options['js_animation'],
'js_slide_direction' => $slideshow_options['js_slide_direction'],
'js_slideshow' => $slideshow_options['js_slideshow'],
'js_slideshow_speed' => $slideshow_options['js_slideshow_speed'],
'js_animation_duration' => $slideshow_options['js_animation_duration'],
'js_direction_nav' => $slideshow_options['js_direction_nav'],
'js_control_nav' => $slideshow_options['js_control_nav'],
'js_keyboard_nav' => $slideshow_options['js_keyboard_nav'],
'js_mousewheel' => $slideshow_options['js_mousewheel'],
'js_prev_text' => $slideshow_options['js_prev_text'],
'js_next_text' => $slideshow_options['js_next_text'],
'js_pause_play' => $slideshow_options['js_pause_play'],
'js_pause_text' => $slideshow_options['js_pause_text'],
'js_play_text' => $slideshow_options['js_play_text'],
'js_randomize' => $slideshow_options['js_randomize'],
'js_slide_start' => $slideshow_options['js_slide_start'],
'js_animation_loop' => $slideshow_options['js_animation_loop'],
'js_pause_on_action' => $slideshow_options['js_pause_on_action'],
'js_pause_on_hover' => $slideshow_options['js_pause_on_hover'],
'js_controls_container' => $slideshow_options['js_controls_container'],
'js_manual_controls' => $slideshow_options['js_manual_controls'],
'js_start_function' => $slideshow_options['js_start_function'],
'js_before_function' => $slideshow_options['js_before_function'],
'js_after_function' => $slideshow_options['js_after_function'],
'js_end_function' => $slideshow_options['js_end_function']
);
foreach ($slideshow_vars as $key => $value) {
if($value == NULL) {
$value = "false";
}
}
print_r($slideshow_vars);
In a number of the values in the array, they are outputting NULL
-- well, I need to change those to a string of false
(this data is being localized and then sent to a JS file which expects false). When I perform the above print_r()
it hasn't actually updated anything.
Upvotes: 0
Views: 311
Reputation: 59709
If all of the keys are the same and you want to save yourself a lot of code, you could try experimenting with this:
$slideshow_vars = array_merge( // Merge two arrays:
// Create an array of the same keys, but all with values of "false"
array_combine(
array_keys( $slideshow_options),
array_fill( 0, count( $slideshow_options), "false")
),
// Remove values that equal false (may need to specify a more precise callback here)
array_filter( $slideshow_options)
);
This should give you the $slideshow_vars
variable you're looking for.
Upvotes: 1
Reputation: 2017
Each loop, $value
is set to the value. By updating the value of $value
, you're just changing it in the local scope, and not setting the value inside that array. For that, you want to reference the field and update it, as such:
foreach ($slideshow_vars as $key => $value) {
if($value == NULL) {
$slideshow_vars[$key] = "false";
}
}
Upvotes: 1
Reputation: 6992
That is because foreach normally passes array fields by value. What you need to do is this:
foreach ($slideshow_vars as $key => &$value) {
if($value == NULL) {
$value = "false";
}
}
Upvotes: 6