2

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;
    }
}
7
  • 1
    Using dd doesn't prove it works. dd ends execution, you'll still get four strings instead of the two you seem to be after (Fender and Gibson) if you use two foreachs. Commented May 6, 2015 at 1:51
  • Yes, you only need the one foreach. The question is why it is coming up as [ in the view. Commented May 6, 2015 at 1:55
  • Is there something wrong with the way I use to generate the brands data? Commented May 6, 2015 at 1:57
  • @cchacholiades what is your desired output? what are you trying to generate in the blade? Commented May 6, 2015 at 1:58
  • Sorry @PawelBieszczad I am trying to access the brands array position 0 and 1, so for example the first time it loops {{b[0]}} outputs Fender and b[1] outputs (2), the second time {{b[0]}} outputs Gibson and b[1] outputs (1) Commented May 6, 2015 at 2:01

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0
@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

4 Comments

Thanks for the response. I tried this and it returns: [ [
I don't see how that is possible given the data present.
I don't have any Laravel setup to test it, but the PHP checks out as proven in my edit.
Add an update with your exact code and a dump of $brand inside the foreach.
0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.