JSON-SE
JSON-SE

Reputation: 11

How to Display Multidimensional array with the same ID (PHP, Laravel)

I have a multidimensional array looked like this.

$arr = [
            [
                ["id" => 1,
                "name" => "Jayson"
                ],
                [
                    "id" => 1,
                    "name" => "Jimmy"
                ]
            ],
            [
                "id" => 2,
                "name" => "Joymae"
            ],
            [
                "id" => 3,
                "name" => "Jasper"
            ]
        ];

I wanted the result to be displayed in html table like this: (PHP) (Laravel)

| ID, name |     
| 1, Jayson |  
| 1, Jimmy |   

| ID, name |     
| 2, Joymae |  

| ID, name |     
| 3, Jayson |

If you have any ideas or solution to this problem please do comment and will be highly appreciated. Thank you.

Upvotes: 0

Views: 308

Answers (2)

Miggy
Miggy

Reputation: 816

You could try this.

@foreach ($variable as $key => $value)
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
        </tr>
        @foreach ($variable as $key => $value)
            <tr>
                <td>$value['id']</td>
                <td>$value['name']</td>
            </tr>
        @endforeach
    </table>
@endforeach

if $value['id'] doesn't work try using $value->id

Upvotes: 2

Bikram Tuladhar
Bikram Tuladhar

Reputation: 9

You can use recursive function to achieve that result

function print(array $arr)
{
    foreach ($arr as $key => $value) {
        if ( is_array($value) ) {
            print($value);
        }else{
            echo $key.'|'.$value."<br>";
        }
    }
}

Upvotes: 0

Related Questions