我为 $result 变量分配了这样的数组对象集合:
// below is the $result value i.e. output of print_r($result);
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [email] => [email protected] ) [1] => stdClass Object ( [email] => [email protected] ) [2] => stdClass Object ( [email] => [email protected] ) [3] => stdClass Object ( [email] => [email protected] ) [4] => stdClass Object ( [email] => [email protected] ) [5] => stdClass Object ( [email] => [email protected] ) ) )
为了将上面的内容转换为简单数组,我使用如下循环:
print_r($result);
$user_emails = array();
foreach ($result as $key => $value) {
array_push($user_emails,$value->email);
}
print_r($user_emails);
我得到的 $user_emails 值如下:
Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] [3] => [email protected] [4] => [email protected] [5] => [email protected] )
我想知道是否有更好更快的方法来实现我上面所做的事情,使用任何 laravel 集合方法。
你可以尝试一下:
$result->pluck('email')->all();
最后这对我有用:
$result->pluck('email')->toArray();