Search code examples
laravellaravel-bladelaravel-views

Laravel Blade: How to loop multidimensional array in view?


I have the below arrays and I want to get it in laravel blade as HTML table.

array:2 [▼
      0 => array:6 [▼
        "room" => "401"
        "landmark" => "Main Building"
        "capacity" => 60
        "faculty" => array:4 [▼
          0 => "DMSM"
          1 => "DMSM"
          2 => ""
          3 => "DMSM"
        ]
        "department" => array:4 [▼
          0 => "BSH"
          1 => "BSH"
          2 => ""
          3 => "BSH"
        ]
        "course" => array:4 [▼
          0 => "PHY 101"
          1 => "PHY 101"
          2 => "Free"
          3 => "PHY 101"
        ]
      ]
      1 => array:6 [▼
        "room" => "504"
        "landmark" => "Main Building"
        "capacity" => 50
        "faculty" => array:4 [▼
          0 => ""
          1 => ""
          2 => "DMSM"
          3 => ""
        ]
        "department" => array:4 [▼
          0 => ""
          1 => ""
          2 => "BSH"
          3 => ""
        ]
        "course" => array:4 [▼
          0 => "Free"
          1 => "Free"
          2 => "PHY 101"
          3 => "Free"
        ]
      ]
    ]

I'm getting the above response from multiple tables now I need to loop in view, so How to loop the above arrays in view??

How can I display the above array values in the below format?

Expected HTML table


Solution

  • Nothing special just work as a normal array and use another loop inside main loop for internal dimensions

    Your array looks like below.

    enter image description here

    I just loop your array inside a controller like below

     $array_values = [
            [
                "room" => "401",
                "landmark" => "Main Building",
                "capacity" => 60,
                "faculty" => [
                    "DMSM",
                    "DMSM",
                    "",
                    "DMSM",
                ],
                "department" => [
                    "BSH",
                    "BSH",
                    "",
                    "BSH",
                ],
                "course" => [
                    "PHY 101",
                    "PHY 101",
                    "Free",
                    "PHY 101",
                ]
            ],
            [
                "room" => "504",
                "landmark" => "Main Building",
                "capacity" => 50,
                "faculty" => [
                    "",
                    "",
                    "DMSM",
                    "",
                ],
                "department" => [
                    "",
                    "",
                    "BSH",
                    "",
                ],
                "course" => [
                    "Free",
                    "Free",
                    "PHY 101",
                    "Free",
                ]
            ]
        ];
    
    
        foreach ($array_values as $values) {
            echo '<br>'; // just a break to show output
            foreach ($values as $key => $value) {
                if (is_array($value)) { // check is value is an array which
                    echo $key . ' are : ';
                    foreach ($value as $val) {
                        echo $val . ' / ';
                    }
                }
                echo $value . '<br>';
    
            }
            echo '<br><br>';
        }
    

    Above code will output something like below

    enter image description here

    What you have to do in blade is something similar to this

     @foreach ($array_values as $array)
        
        
        @foreach($values as $key => $value)
        
          @if (is_array($value))
    
             @foreach ($value as $val) 
    
                   {{$val}}
    
             @endforeach
                    
          @endif
    
           {{$value}}
        
       @endforeach
    
     @endforeach