从多个阵列cakephp中删除元素

时间:2016-04-11 16:58:36

标签: php arrays cakephp

是否有一种从CakePHP数组中删除Subelement的简单方法,例如foreach ($junta)? 我有这个数组$junta有多种元素,我想删除[password] => 3d0f93c607cfe638f9421d4af49a3455

使用此代码:

$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] => antonioflores30@gmail.com
                    [role] => king
                    [modified] => 2016-03-13 02:21:39
                    [status] => 1
                )

        )

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

3 个答案:

答案 0 :(得分: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(...)
    -> ....
));

答案 1 :(得分:0)

要仅删除password字段,请尝试此

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

答案 2 :(得分:0)

在您的示例中,您似乎试图取消设置值而不是取消设置值的键。对于您所展示的确切示例,这应该有效:

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