cch
cch

Reputation: 3386

Laravel - can't get values from multidimensional array with blade

I am having trouble looping through a multidimensional array with blade in laravel. I am sending the data from the controller like so:

return View::make('store.categories')
            ->with('brands', $brands);

And if I die dump the data:

array (size=2)
  0 => 
    array (size=2)
      0 => string 'Fender' (length=6)
      1 => string '(2)' (length=3)
  1 => 
    array (size=2)
      0 => string 'Gibson' (length=6)
      1 => string '(1)' (length=3)

I've tried to use two @foreach loops but I couldn't get it to work:

@foreach($brands as $brand)
  @foreach($brand as $b)
  {{$b}}
  @endforeach
@endforeach

The above will output: Fender (2) Gibson (1).


I tried to get the 0 value for the $b to output Fender but it just prints the 0 position character for each of the items in the $b array:

@foreach($brands as $brand)
  @foreach($brand as $b)
  {{$b[0]}}
  @endforeach    
@endforeach

The above will output F ( G (.


In my controller if I do:

foreach ($brands as $b) {
    foreach($b as $key=>$v) {
       dd($v);
    }
}

it will output string 'Fender' (length=6), which seems like the second loop inside the first @foreach works. Although, when it comes to the blade code mentioned above it doesn't.

I'm probably doing something terribly wrong. How can I get the output for the values 0 and 1 for the nested arrays individually? Any help is highly appreciated.


This is how I create the data in my controller's function:

$products = Product::with('brand')->whereIn('category_id', $children->lists('id'));
$brand_ids = array();
$brands = array();

foreach ($products->get() as $p) {
    $brand_ids[] = $p->brand_id;
}
$brand_count = array_count_values($brand_ids); 
foreach ($brand_count as $key=>$value) {
    $query = Brand::where('id', '=', $key)->lists('name');
    // dd($query);
    foreach($query as $key=>$name) {
        $array = array(
             $name,
             '('.$value.')'
            );
        $brands[] = $array;
    }
}

Upvotes: 2

Views: 10486

Answers (3)

ssi-anik
ssi-anik

Reputation: 3694

as you want to get the 2nd level values of multi dimensional array, and if you know that 2nd level will have only two values, then why don't you try

@foreach($brands as $brand) 
    {{$brand[0]}} {{$brand[1]}}
@endforeach 

Upvotes: 0

Pawel Bieszczad
Pawel Bieszczad

Reputation: 13325

Controller

$brands = Brand::whereIn('id', $brand_ids)->lists('name', 'id');

Blade

@foreach($brands as $id => $brand)
    Id: {{$id}}, Brand: {{$brand}}
@endforeach

This should work and save you performance, cause we query all the brands instead of each one individually. A better approach would be to have the products relation set up and get them that way.

Upvotes: 5

Devon Bessemer
Devon Bessemer

Reputation: 35337

@foreach($brands as $brand)
  {{$brand[0]}}
@endforeach

You need to look at how the array is organized. Once you dig into the first level, 'Fender' is at offset [0] and '(2)' is at offset [1], so you only need one foreach.

The reason you were getting F is because you were getting the offset [0] on the string 'Fender' (or in other words, the first letter) because the second foreach was bringing you four strings, not arrays.

$brands = [
  0 => 
    [
      0 => 'Fender',
      1 => '(2)'
    ],
  1 => 
    [
      0 => 'Gibson',
      1 => '(1)'
    ]
];

var_dump($brands);

foreach($brands as $brand) {
  echo $brand[0]."\n";
}

Outputs:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "Fender"
    [1]=>
    string(3) "(2)"
  }
  [1]=>
  array(2) {
    [0]=>
    string(6) "Gibson"
    [1]=>
    string(3) "(1)"
  }
}
Fender
Gibson

Upvotes: 0

Related Questions