I have $result variable assigned with collection of array objects like this :
// below is the $result value i.e. output of print_r($result);
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [email] => test@test.com ) [1] => stdClass Object ( [email] => test@test.com ) [2] => stdClass Object ( [email] => test@test.com ) [3] => stdClass Object ( [email] => test@test.com ) [4] => stdClass Object ( [email] => test@test.com ) [5] => stdClass Object ( [email] => test@test.com ) ) )
In order to covert above to simple array I am using loop like below :
print_r($result);
$user_emails = array();
foreach ($result as $key => $value) {
array_push($user_emails,$value->email);
}
print_r($user_emails);
and I get $user_emails value as below :
Array ( [0] => test@test.com [1] => test@test.com [2] => test@test.com [3] => test@test.com [4] => test@test.com [5] => test@test.com )
I want to know if there is better and faster way to achieve what I have done above, using any of the laravel collection method.
finally this worked for me :
$result->pluck('email')->toArray();