数组推送到多维数组

时间:2014-10-17 05:48:11

标签: php arrays loops multidimensional-array

我是php和数组的新手。我有一个名为$ get的数组,其中包含:

array (size=5)
0           => 
array (size =3)
'name'    => string '6Jj3sHDG2Dciq92P0fELyw==' (length                    =24)
'email'   => string 'uYyYxVif7yOSO+nxLXRoKxj8oulFOp9EONDvMXC+zE8=' (length=44)
'password'=> string 'umxCWS0OXGTomcDWkHZUCA==' (length                    =24)

1           => 
array (size =3)
'name'    => string 'GjtDUw6NwmjQuoG/lwWYcg==' (length                    =24)
'email'   => string 'gHi5V7tzYABdlb1iCr8Tuw==' (length                    =24)
'password'=> string 'umxCWS0OXGTomcDWkHZUCA==' (length                    =24)

2           => 
array (size =3)
'name'    => string 'PB/6qLhQ/xe8iRmjWglb8g==' (length                    =24)
'email'   => string 'ZvTXUau05ubgzOEn/cY0XQ==' (length                    =24)
'password'=> string 'umxCWS0OXGTomcDWkHZUCA==' (length                    =24)

3           => 
array (size =3)
'name'    => string 'nYFzzMaZxZ7F5zV9jE7X5A==' (length                    =24)
'email'   => string '0oyJhuD9u5PHLku+wV9xhQ==' (length                    =24)
'password'=> string 'umxCWS0OXGTomcDWkHZUCA==' (length                    =24)

4           => 
array (size =3)
'name'    => string 'XEJyjRWo0jKt4XjSRct6/A==' (length                    =24)
'email'   => string 'JQyW/v9RATiJs8m9QwPRwA==' (length                    =24)
'password'=> string 'umxCWS0OXGTomcDWkHZUCA==' (length                    =24)

我正在循环每一个来解密它。如何将其保存到数组中或用解密的数据覆盖数组本身?这是我如何解密和循环它。

    $get = Users::getAll();
    $decr= new Cipher("somekey");
    foreach( $get as $result )
    {
        //Decryption
        $new_decrypted_name    = $decr->decrypt($result['name']);
        $new_decrypted_email   = $decr->decrypt($result['email']);
        $new_decrypted_password= $decr->decrypt($result['password']);
    }

我也搜索了,但似乎他们有不同的实现。谢谢!

2 个答案:

答案 0 :(得分:1)

你可以这样做:

    $get = Users::getAll();
    $decr = new Cipher("somekey");
    foreach( $get as $key => $result )
    {    
       $get[$key]['name'] = $decr->decrypt($result['name']);
       $get[$key]['email'] = $decr->decrypt($result['email']);
       $get[$key]['password'] = $decr->decrypt($result['password']);
    }

答案 1 :(得分:0)

像这样你可以动态地做到这一点

$decrypted = [];
 foreach ($get as $key => $result) {
  foreach ($result as $input => $value) {
    $decrypted[$key][$input]=  $decr->decrypt($value);
  }
}