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
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;

 

}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.