3

I am quite new no laravel. I know that it is a pretty basic question. But, I still can't figure it out. Heres my Array output and I want to get the value of name from within this array. This is the output I get in postman after I used print_r:

Array
(
    [0] => Array
        (

            [name] => Test 2322              
            [id] => 4
        )

)
Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32

5 Answers5

7

if you want all of them

foreach ($datas as $datavals) {
       echo $datavals['name'];
}

If you want 0 array name element value Just call following :

   echo $memus[0]['name'];
Karthik
  • 5,589
  • 18
  • 46
  • 78
7

In case this is a collection you can use the pluck method

$collection = collect([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$plucked = $collection->pluck('name');

$plucked->all();
// ['Desk', 'Chair']

If in your case you do not have a collection you can create it with the collect method.

In your case:

$myarray = collect($initialArray); //You can ignore this if it is already an array

$nameArray = $myarray->pluck('name')->all();

foreach($nameArray as $name)
{
    echo $name; //Test 2322
}
ka_lin
  • 9,329
  • 6
  • 35
  • 56
1

You can iterate the array with foreach on blade and get index="name" for each entry like this:

In View

@foreach($data as $d)

   {{$d['name']}}

@endforeach

In Controller

foreach($data as $d){

   // This is the value you want
   $name = $d['name']

}
Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
1

In newest version of laravel you can use the Arr::pluck() helper function to take an array of all names values.

In your case

Arr::pluck($array, 'name')

Will output

['Test 2322']

0
Simply write the array name with the indices and key which have to access.Suppose $a[] is array then $a[0]['name'] and the value at zero index of array will be retrieved or you can parse it in loop which will give the value of key ['name'] at every indices. 
foreach($a as $item)
{
  print_r($item['name']);
}
stuti
  • 1
  • 2