Jump to content

array references php 5


vurentjie

Recommended Posts

hi just a quick question,

 

some code for what I am trying...

further up .....
self::$_menus['test'] = array();
self::loadLinks($links,'test');

public static function loadLinks($links){
    $linkmark = false;
    $where = array_slice( func_get_args(), 1 );
    foreach($where as $resource){
        $linkmark=($linkmark===false)?self::$_menus[$resource]:$linkmark[$resource];
   }			

$linkmark['newlink']='testinglink';
var_dump($linkmark);
var_dump(self::$_menus);


}		


 

it seems the menu array is not being passed by reference, if I convert all static class vars and use &$this->.... I can reference properly....

 


$this->_menus['test'] = array();
$this->loadLinks($links,'test');

public function loadLinks($links){
    $linkmark = false;
    $where = array_slice( func_get_args(), 1 );
    foreach($where as $resource){
        $linkmark=($linkmark===false)?&$this->_menus[$resource]:&$linkmark[$resource];
}			

$linkmark['newlink']='testinglink';
var_dump($linkmark);
var_dump($this->_menus);


}		


 

 

I thought php5 would handle this as reference? Can anyone give me a why?

Link to comment
https://forums.phpfreaks.com/topic/160288-array-references-php-5/
Share on other sites

well i ended up just sticking to the follwing...works fine...

public function loadLinks($links){

 

$linkmark = false;

$where = array_slice( func_get_args(), 1 );

 

foreach($where as $resource)($linkmark===false)? $linkmark =  &$this->_menus[$resource] : $linkmark = &$linkmark[$resource];

 

$linkmark = ( is_array($linkmark) ) ? array_merge($linkmark,$links) : $links;

 

}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.