首页 > 解决方案 > How to pull each game value if main value if true JSON in PHP

问题描述

PHP File for getting json value

$url = 'demo.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters =  json_decode($data, true); // decode the JSON feed
<table>
    <tbody>
        <tr>
            <th>id</th>

        </tr>
        <?php foreach ($characters['events'][0]['offers'][0]['outcomes'] as $character) : ?>
        <tr>
            <td> <?php echo $character['id']; ?></td>

        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

Thank you for the solutions. only need to show data if main value is true from each game. Thanks Dhamo

标签: phpjson

解决方案


在 for 循环中检查主要条件

  <?php 

    foreach ($characters['events'] as $events){
    foreach ($events['offers'] as $offers){
        if($offers['main']==true || $offers['main']==1){
            foreach ($offers['outcomes'] as $character) : ?>
                <tr>
                    <td> <?php echo $character['id']; ?></td>
                    <td> <?php echo $character['label']; ?></td>    
                    <td> <?php echo $character['line']; ?></td>
                    <td> <?php echo $character['oddsAmerican']; ?></td>
                    <td> <?php echo $character['participant']; ?></td>
                </tr>
        <?php endforeach;

    }
}
}?>

推荐阅读