In this tutorial you will learn about the Laravel Arr pluck() function Example and its application with practical example.
Laravel array pluck() function Example
In this article, I’ll show you how to use laravel array pluck() function with example. We will show example of array pluck() function in laravel. In this tutorial, we will use array pluck() function to retrieves all of the values for a given key from an array.
Laravel Arr::pluck method
The Arr::pluck method retrieves all of the values for a given key from an array.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\FileController; use Illuminate\Support\Arr; class HomeController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { $array1 = [ ['fruit' => ['id' => 1, 'name' => 'apple']], ['fruit' => ['id' => 2, 'name' => 'banana']], ]; $names1 = Arr::pluck($array1, 'fruit.name'); print_r($names1); echo "<br>"; $array2 = [ ['product' => ['id' => 1, 'name' => 'Pen', 'price' => '100']], ['product' => ['id' => 2, 'name' => 'Book', 'price' => '200']], ]; $names2 = Arr::pluck($array2, 'product.price'); print_r($names2); } } |
Output:-
1 2 |
Array ( [0] => apple [1] => banana ) Array ( [0] => 100 [1] => 200 ) |