dongpiansui8755
2017-03-30 04:51
浏览 3.0k
已采纳

Laravel:从集合数组中删除元素

i use a variable called $users to "pluck" only the emails from my users table, using:

$users = user::all()->pluck('email');

when i dd($users), i get this

<pre>
Collection {#214 ▼
  #items: array:8 [▼
    0 => "adminone@example.com"
    1 => "admintwo@example.com"
    2 => "Adobot@example.com"
    3 => "Lawly@example.com"
    4 => "Cerms@example.com"
    5 => "Charlie@example.com"
    6 => "test@example.com"
    7 => "test001@example.com"
  ]
}
</pre>

meaning that $users isn't purely an array, i get various types of errors when i treat it like an array.

my question is, in php (in a Laravel Framework) how do i remove a specific user's email from the variable above? or is there a better way to use "pluck" so that it returns an array instead?

thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我使用名为$ users的变量来“拔除”来自我的用户表的电子邮件,使用:

$ users = user :: all() - &gt; pluck('email');

当我dd($ 用户),我得到这个

 &lt; pre&gt; 
Collection {#214▼
 #items:array:8 [▼
 0 =&gt;  “adminone@example.com”
 1 =&gt;  “admintwo@example.com”
 2 =&gt;  “Adobot@example.com”
 3 =&gt;  “Lawly@example.com”
 4 =&gt;  “Cerms@example.com”
 5 =&gt;  “Charlie@example.com”
 6 =&gt;  “test@example.com”
 7 =&gt;  “test001@example.com”
] 
} 
&lt; / pre&gt; 
   
 
 

意味着$ users不是纯粹的数组,我得到各种各样的 当我把它当作一个数组来处理它时的错误类型。

我的问题是,在php(在Laravel框架中)如何从上面的变量中删除特定用户的电子邮件? 或者有更好的方法来使用“pluck”以便它返回一个数组吗?

提前感谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douxu3315 2017-03-30 09:33
    已采纳

    I had this problem too and i solved it like this:

    DB::table('user')->pluck('email')->all();

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongqin6926 2017-03-30 04:55

    Here $users is an Std Class Object, you can iterate over it and push its properties to an array like:

    $userDetails = array();
    foreach($users as $user)
    {
      $userDetails[] = (array)$user;
      // or
      $userDetails[] = $user->index;
    }
    

    You can also put an If block inside foreach() to check some condition.

    评论
    解决 无用
    打赏 举报
  • dsfsdfsdfsdfsdf45454 2017-03-30 05:34

    If you want to remove email where email id where index is 0 then you follow below query first

       $users = user::all()->pluck('email');
       unset($users['0']);
       $dd($users);
    
    评论
    解决 无用
    打赏 举报
  • dongshubang7816 2017-03-30 05:38

    You would want to use ->forget()

    $collection->forget($key);
    

    Or you can use reject method

    $newColection = $collection->reject(function($element) {
        return $element === 'emailaddress@';
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题