Laravel 10 Arr::pluck() Helper Function Use Example

  • 03-03-2023
  • 2254
  • Laravel 10
  • Haresh Chauhan

How to use Arr::pluck() helper function in laravel 10, In this comprehensive post I will tell you about the laravel 10 helper function Arr::pluck(), we do no more required to use the array_column() php function. as well as we can also use a multidimensional array with "DOT" notation.

You may also access a multidimensional array inside keys using "." Dot notation. Using the array pluck helper function you may get an array column value while you working with multiple arrays. This helper function also may use to find specific array key values.

In Laravel 10, you can use the pluck() method to pluck the values of a given key from an array of arrays. It returns the values from a single column of the array.

Usage of Arr::pluck() example, I have given different examples, by reading this post example you will be very clear about this helper function. I have given examples and how they output in this post given. This helper function also performs the array_column() php function role, as well as some additional tricks of use this function also details noted.

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
]

We always thanks to you for reading our blogs.


dharmesh-image

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-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :