Laravel:从集合数组中删除元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43108801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel : Removing an element from a collection array
提问by Kevin fu
i use a variable called $users to "pluck" only the emails from my users table, using:
我使用一个名为 $users 的变量来“提取”我的用户表中的电子邮件,使用:
$users = user::all()->pluck('email');
$users = user::all()->pluck('email');
when i dd($users), i get this
当我 dd($users) 时,我得到了这个
<pre>
Collection {#214 ▼
#items: array:8 [▼
0 => "[email protected]"
1 => "[email protected]"
2 => "[email protected]"
3 => "[email protected]"
4 => "[email protected]"
5 => "[email protected]"
6 => "[email protected]"
7 => "[email protected]"
]
}
</pre>
meaning that $users isn't purely an array, i get various types of errors when i treat it like an array.
这意味着 $users 不是纯粹的数组,当我将它视为数组时,我会遇到各种类型的错误。
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?
我的问题是,在 php 中(在 Laravel 框架中)如何从上面的变量中删除特定用户的电子邮件?或者有没有更好的方法来使用“pluck”,以便它返回一个数组?
thanks in advance.
提前致谢。
采纳答案by gorgonauta
I had this problem too and i solved it like this:
我也有这个问题,我是这样解决的:
DB::table('user')->pluck('email')->all();
DB::table('user')->pluck('email')->all();
回答by Mayank Pandeyz
Here $users
is an Std Class Object, you can iterate over it and push its properties to an array like:
这$users
是一个 Std 类对象,您可以遍历它并将其属性推送到一个数组,如:
$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.
你也可以If
在里面放一个块foreach()
来检查一些条件。
回答by ram pratap singh
If you want to remove email where email id where index is 0 then you follow below query first
如果您想删除电子邮件 ID 索引为 0 的电子邮件,那么您首先要遵循以下查询
$users = user::all()->pluck('email');
unset($users['0']);
$dd($users);