Understanding an interesting array update/replace function
- by dave
I'm a PHP amateur.
This array function is an adaption of a function I noticed while reading this article.
I thought this was an interesting type of array function, but I have a question about the way it works.
my_func( array( 'sky' => 'blue' ) );
function my_func( array $settings = array() )
{
$settings = $settings + array( 'grass'=>'green','sky'=>'dark' );
print_r( $settings ) ;
// outputs: Array ( [sky] => blue [grass] => green )
}
but.....................
my_func( array( 'sky' => 'blue' ) );
function my_func( array $settings = array() )
{
$settings = array( 'clock'=>'time' ) ;
$settings = $settings + array( 'grass'=>'green','sky'=>'dark' );
print_r( $settings ) ;
// outputs: Array ( [clock] => time [grass] => green [sky] => dark )
}
Why does [sky] not equal 'blue' in the second instance?
Thanks.