2016-04-11 33 views
0

有沒有簡單的方法從CakePHP數組中刪除子元素,例如foreach ($junta)? 我有這個陣列$junta與diverses元素,我想刪除[password] => 3d0f93c607cfe638f9421d4af49a3455從多個數組中刪除元素cakephp

使用此代碼:

$index = array_search('password',$junta); 
if($index !== FALSE){ 
    unset($junta[$index]); } 

並不起作用。的$junta

值:

Array 
(
    [junta] => Array 
     (
      [Council] => Array 
       (
        [id] => 21 
        [descripcion] => esta es una prueba 7 
        [version] => 
        [imagen] => Cuadernillo de higiene y seguridad Industrial.pdf 
        [archivo] => 21 
        [director] => 
        [fecha] => 2016-04-07 
        [user_id] => 1 
       ) 

      [User] => Array 
       (
        [id] => 1 
        [username] => jflores 
        [password] => 3d0f93c607cfe638f9421d4af49a 
        [nombres] => Antonio 
        [Apellido_P] => flores lara 
        [Apellido_M] => 
        [created] => 2016-03-13 01:57:37 
        [email] => [email protected] 
        [role] => king 
        [modified] => 2016-03-13 02:21:39 
        [status] => 1 
       ) 

     ) 

    [consejeros] => Array 
     (
      [0] => Array 
       (
        [CouncilorsCouncil] => Array 
         (
          [councilor_id] => 2 
          [council_id] => 21 
         ) 
       ) 
     ) 
) 

回答

0

只需選擇您需要的領域。

CakePHP的2

$this->MyModel->find('All',array(
    'fields' => array('User.username','User.email',....), 
    'conditions' => ... 
)); 

CakePHP的3

$this->MyModel->find() 
    ->select(['User.username','User.email',....]) 
    ->where(...) 
    -> .... 
)); 
0

要刪除僅password場試試這個

$result = Hash::remove($arr2, 'junta.User.password'); 
0

在你的榜樣它看起來像你想取消設置值而不是取消設置的關鍵值。對於確切的例子,你已經表明這應該工作:

if(isset($junta['junata']['User']['password'])){ unset($junta[junata]['User']['password']); }