Search code examples
phparraysjsonmultidimensional-array

How to check non-constant array


when I get my data series with foreach, I get the result in the structure below

in the structure [N]==1 match result 1 win [N]==2 match result ends in a draw [N]==3 match result away win [O] parts are the rates

if we look at the structure, Barcelona Realmadrid match has mr1 mrx mr2, but Juventus - Salzburg match ms1 and msx are not given, only ([N]=3)ms2 is given

`

Array
(
    [teams] => Barcelona Realmadrid
    [odds] => Array
        (
            [matchresult] => Array
                (
                    [0] => Array
                        (
                            [N] => 1
                            [O] => 1.25
                        )

                    [1] => Array
                        (
                            [N] => 2
                            [O] => 4
                        )

                    [2] => Array
                        (
                            [N] => 3
                            [O] => 6
                        )

                )


        )

)
Array
(
    [teams] => Juventus - Salzburg
    [odds] => Array
        (
            [matchresult] => Array
                (
                    [0] => Array
                        (
                            [N] => 3
                            [O] => 8
                        )


                )


        )

)

`

How can I get the result I want with a filtering structure I want?

teams mr1 mrx mr2
Barcelona - Realmadrid 1.25 4 6
Juventus - Salzburg - - 8

Solution

  • Presuming you have your inbound data as an array, something like this:

    $r = [
        [
            'teams' => 'Barcelona Realmadrid',
            'odds' => [
                'matchresult' => [
                    [
                        'N' => 1,
                        'O' => 1.25,
                    ],
                    [
                        'N' => 2,
                        'O' => 4,
                    ], 
                    [
                        'N' => 3,
                        'O' => 6,
                    ]
                ]
            ]
        ],
        [
            'teams' => 'Juventus - Salzburg',
            'odds' => [
                'matchresult' => [
                    [
                        'N' => 3,
                        'O' => 8,
                    ]
                ]
            ]
        ]
    ];
    

    Then you should just be able to loop though your array to produce the table output:

    <table>
        <thead>
            <tr>
                <th>teams</th>
                <th>mr1</th>
                <th>mrx</th>
                <th>mr2</th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($r as $teamResults) {
                $mr = array_column($teamResults['odds']['matchresult'], 'O', 'N');
                ?>
                <tr>
                    <td><?php echo htmlspecialchars($teamResults['teams']); ?></td>
                    <td><?php echo htmlspecialchars($mr[1] ?? '-'); ?></td>
                    <td><?php echo htmlspecialchars($mr[2] ?? '-'); ?></td>
                    <td><?php echo htmlspecialchars($mr[3] ?? '-'); ?></td>
                </tr>
                <?php
            }
            ?>
        </tbody>
    </table>