swatch360
swatch360

Reputation: 180

Make multiple arrays into a row

I'm not sure how to go about this.

I have an array which looks likes this:

Please help!

 Array
 (
    [0] => Array
        (
            [name] => Admin
            [surname] => Coetzee
            [products] => Array
                (
                    [1] => Array
                        (
                            [0] => shoes xl
                            [1] => 12
                            [2] => 2809000000020
                        )

                [2] => Array
                    (
                        [0] => belts men
                        [1] => 15
                        [2] => 2809000000021
                    )

                [3] => Array
                    (
                        [0] => swatch watch
                        [1] => 
                        [2] => 2809000000035
                    )

                [4] => Array
                    (
                        [0] => shoes medium
                        [1] => 18
                        [2] => 2809000000036
                    )

            )

    )

[1] => Array
    (
        [name] => Anneleen
        [surname] => Van der Merwe
        [products] => Array
            (
                [1] => Array
                    (
                        [0] => shoes xl
                        [1] => 12
                        [2] => 2809000000023
                    )

                [2] => Array
                    (
                        [0] => belts men
                        [1] => 15
                        [2] => 2809000000024
                    )

            )

)

I would like to out put it in a table like this for example.

With each item in array being a row on the table.

<td>Admin coetzee</td>
<td>shoes xl</td>
<td>2809000000020</td> 
<td>Admin coetzee</td>
<td>belts men</td>
<td>2809000000021</td> 
<td>Anneleen</td>
<td>shoes xl</td>
<td>2809000000023</td> 

Here's the code I already have:

   $result = DB::select('SELECT   
    users.id, users.name, users.surname,
    applications.table_assigned,
    vendor_pricelist.products

    FROM ((users INNER JOIN applications ON users.id =    

    applications.user_id)
    INNER JOIN vendor_pricelist ON users.id =  
    vendor_pricelist.user_id)
    WHERE applications.event_id =?', [$event_id]);

    $counter = 0;

    $custom_array = [];

    foreach ($result as $value) {

        $custom_array[$counter] = array(

            'name' => $value->name,
            'surname' => $value->surname,
            'products' => (array)json_decode($value->products)

        );

        $counter++;

    }
    $result = $custom_array;

Upvotes: 0

Views: 48

Answers (1)

Mihai Matei
Mihai Matei

Reputation: 24276

Please note that this code is not tested so you may do some adjustments in order to make it work properly:

<table>
    <thead>
        <tr>
            <td>Name</td>
            <td>Product name</td>
            <td>Product identifier</td>
        </tr>
    </thead>
    <tbody>

@foreach ($result as $value)
    <tr>
        <td rowspan="{{count($value['products'])}}">{{$value['name']}} {{$value['surname']}}</td>

    @foreach ($value['products'] as list($productName, $productId, $productIdentifier))
        <td>{{$productName}}</td>
        <td>{{$productIdentifier}}</td>
    </tr>
    <tr>
    @endforeach

    </tr>
@endforeach

    </tbody>
</table>

Upvotes: 1

Related Questions