0

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

Shaikhul Saad
  • 177
  • 2
  • 2
  • 12

2 Answers2

1

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
Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
1

Solution Finally got the solution.

<table>
@foreach($slot_wise_routine as $routine)
    <tr>
        <td>{{ $routine['landmark'] }}</td>
        <td>{{ $routine['room'] }}</td>
        <td>{{ $routine['capacity'] }}</td>
        @foreach($routine['course'] as $key => $course)
            <td>
                {{ $course }} <br> {{ $routine['faculty'][$key] }} <br> {{ $routine['department'][$key] }}
            </td>
        @endforeach
    </tr>
@endforeach
Shaikhul Saad
  • 177
  • 2
  • 2
  • 12