Laravel Arr::pluck() helper function retrive all of the value form given array key from an array.
There where always use atleast 2 argument pass. Arr::pluck() helper function also you can use with multidimentional array with dot notation. you can find key value from nested key value using "DOT" notation.
This pluck method also you can use in laravel eloquent, and laravel collection.
Pluck method extract value from given array, and always it will return an array. There is no any complex process to use it is provider by laravel helper function.
Some examples below are given regarding array pluck you can refer to and apply in your application easily.
Exmaple 1: Use Simple Arr::pluck()
Simply you can easily use with an array, in this Arr::pluck() two-argument passed, in that first is an array and second is which array you want to retrieve that key.
In this example, I have to retrieve the name from the array using the array pluck laravel helper function.
use Illuminate\Support\Arr; $array = [ ['name' => 'Jone', 'age' => 23], ['name' => 'Mike', 'age' => 18], ['name' => 'Jordan', 'age' => 31], ['name' => 'Rickey', 'age' => 28], ['name' => 'Russle', 'age' => 21], ]; $names = Arr::pluck($array, 'name'); // OUTPUT array:5 [▼ 0 => "Jone" 1 => "Mike" 2 => "Jordan" 3 => "Rickey" 4 => "Russle" ]
Exmaple 2: Use Arr::pluck() With Dot Notation
In this example, I have passed a multi-dimensional array using "DOT" notation easy to find out the developer name from the multidimensional array. also, you can still live multidimensional array inside developer.
use Illuminate\Support\Arr; $array = [ ['developer' => ['name' => 'Jone', 'age' => 23]], ['developer' => ['name' => 'Mike', 'age' => 18]], ['developer' => ['name' => 'Jordan', 'age' => 31]], ['developer' => ['name' => 'Rickey', 'age' => 28]], ['developer' => ['name' => 'Russle', 'age' => 21]], ]; $names = Arr::pluck($array, 'developer.name'); // OUTPUT array:5 [▼ 0 => "Jone" 1 => "Mike" 2 => "Jordan" 3 => "Rickey" 4 => "Russle" ] $age = Arr::pluck($array, 'developer.age'); array:5 [▼ 0 => 23 1 => 18 2 => 31 3 => 28 4 => 21 ]
Example 3: User Arr::pluck() With List Key
In this last example you can see that I have passed the third argument in Arr::pluck() helper function, which will return list.name as key and age as value.
use Illuminate\Support\Arr; $array = [ ['developer' => ['name' => 'Jone', 'age' => 23]], ['developer' => ['name' => 'Mike', 'age' => 18]], ['developer' => ['name' => 'Jordan', 'age' => 31]], ['developer' => ['name' => 'Rickey', 'age' => 28]], ['developer' => ['name' => 'Russle', 'age' => 21]], ]; $age = Arr::pluck($array, 'developer.age','developer.name'); // OUTPUT array:5 [▼ "Jone" => 23 "Mike" => 18 "Jordan" => 31 "Rickey" => 28 "Russle" => 21 ]
So you can see, How to use the array luck helper function in laravel.
If you are familier with core php then you definitly use array_column() function that also will return same result.
But here laravel is given notation and multidimensional array facility for a helper function. and modern helper function. that will make your site soft.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Know All About Laravel Mixin Use In Vue Js With Example
- Arr::crossJoin() | Laravel Helper Function
- Select2 ajax example
- Import Export Database in JSON Format Laravel
- laravel cache clear command
- Laravel Most Useful Command's Lists With Parameters Example
- Arr::forget() | Laravel Helper Function
- Call to undefined function Gregwar\Captcha\imagecreatetruecolor()
- could not find driver (SQL: select count(*) as aggregate from "users" where "email" = admin@gmail.com)
- Laravel One to Many Eloquent Relationship Tutorial With Example