2014-01-24 60 views
2
$new_array_user_id = array('user_id' => $this->Session->read('UserAuth.User.id')); 

輸出:合併數組在CakePHP

Array 
(
    [user_id] => 19 
) 

這裏previos陣列碼:

$prevSessionData = $this->Session->read('form.data'); 
Array 
(
    [Project] => Array 
     (
      [first_name] => vikas 
      [last_name] => tyagi 
     ) 

) 

合併數組:

$currentSessionDatas = Hash::merge((array) $new_array_user_id, $prevSessionData); 
Array 
(
    [user_id] => 19 
    [Project] => Array 
     (
      [first_name] => vikas 
      [last_name] => tyagi 
     ) 

) 

需要輸出喜歡的是:

Array 
    (

     [Project] => Array 
      ( 
       [user_id] => 19 
       [first_name] => vikas 
       [last_name] => tyagi 
      ) 

    ) 

回答

2
$new_array_user_id = array(
    'Project' => array(
     'user_id' => $this->Session->read('UserAuth.User.id')) 
); 
2
$new_array_user_id['Project']['user_id']=$this->Session->read('UserAuth.User.id');