Understanding an interesting array update/replace function

Posted by dave on Stack Overflow See other posts from Stack Overflow or by dave
Published on 2010-12-31T02:50:47Z Indexed on 2010/12/31 2:54 UTC
Read the original article Hit count: 334

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays