Align data as an array for tournament bracket php json

Issue

I have a bracket data and and it accepts data in the follwing form given blow

[// Round 1 bracket
    [// row 1
        {
            "name" : 'test name',
            'id'   : 1
        }, {
            "name" : 'test last',
            'id'   : 2
        }
    ], [ row 2
        {
            "name" : 'test name',
            'id'   : 1
        }, {
            "name" : 'test last',
            'id'   : 2
        }
    ]
], [// Round 2 Bracket
    [ //row 1
        {
            "name" : 'test name',
            'id'   : 1
        }, {
            "name" : 'test last',
            'id'   : 2
        }
    ]
]

but the main problem is that on json I have to create multi dimentional array and the data is getting from mysqli which is 1 table

| player_id  | player_name | participant_id | participant_name | round |
| ---------- | ----------- | -------------- | ---------------- | ----- |
| 1          | Mark Alan   | 3              | Steve            | 1     |
| 2          | John Doe    | 4              | Sean Henshaw     | 1     |
| 1          | Mark Alan   | 2              | John Doe         | 2     |

so how do I achieve this how do i make in this format with foreach loop I am unable to set data with this format here is the sample php code i created with this it is possible to output json data but while looping thourgh database query how do I arrange data in array form to set for brackets

$data = array(
array(
    array(
        'player_id'  => 1,
        'player_name'   => 'Mark Alan',
        'round' => 1
    ), array(
        'participant_id'   => 3,
        'participant_name'   => 'Steve',
        'round' => 1
    )
), array(
    array(
        'player_id'  => 2,
        'player_name'   => 'John Doe',
        'round' => 1
    ), array(
        'participant_id'   => 4,
        'participant_name'   => 'Sean Henshaw',
        'round' => 1
    )
) 
), array(
    array(
        array(
            'player_id'  => 1,
            'player_name'   => 'Mark Alan',
            'round' => 2
        ), array(
            'participant_id'   => 2,
            'participant_name'   => 'John Doe',
            'round' => 2
        )
    )
);

so Above is the output which i placed by my self nwo with running a single loop capturing data from the databse within single loop how can i make above formatted data I used the given code below but when it is next round a new array should be created then round 3 and so on

$bracket = array();
foreach($data as $key => $dt) {
    $bracket[] = array(
        array(
            'name' => $dt['player_name'],
                'id' => $dt['player_id'],
        ),array(
            
            'name' => $dt['participant_id'],
                'id' => $dt['participant_name'],
        )
    );
}

This above code prints me correct data but i am not been able to seperate the rounds

[
    [
        {"name":"Mark Alan","id":1},
        {"name":3,"id":"Steve"}
    ], 
    [
        {"name":"John Doe","id":2},
        {"name":4,"id":"Sean Henshaw"}
    ],  
    [
        {"name":"Mark Alan","id":1},
        {"name":2,"id":"John Doe"}
    ]
]

Solution

You should map your database results to new array:


$data = [];

while ($row = mysqli_fetch_array($stmt, MYSQLI_ASSOC)) {
   $data[$row['round']] ??= [];

   $data[$row['round']][] = [
        [
            'name' => $row['player_name'],
            'id' => $row['player_id'],
        ],
        [
            'name' => $row['participant_name'],
            'id' => $row['participant_id'],
        ],
   ];
}

$dataJson = json_encode($data);

Answered By – Justinas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published