Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

Undefined array key 0 in

Hello i am trying to get values from API Json and print it in HTML

this is my html code:

<?php include('api.php');?>

<div class="table-list">
    <?php
    if (empty($data)) {
        // handle empty data
    } else {
        $valuesPerPage = 8;
        $totalPages = ceil(count($data) / $valuesPerPage);

        if (isset($_GET['page'])) {
            $currentPage = $_GET['page'];
        } else {
            $currentPage = 1;
        }

        $startIndex = ($currentPage - 1) * $valuesPerPage;
        $endIndex = $startIndex + $valuesPerPage;

        $count = ($currentPage - 1) * $valuesPerPage + 1;

        for ($i = $startIndex; $i < $endIndex && $i < count($data); $i++) {
            $game = $data[$i];
            ?>
            <div class="row list-item">
                <div class="col-4">
                    <h4 class="mb-0 font-d-bld text-white"><?php echo $count++; ?>. <?php echo $game['name']; ?></h4>
                </div>
                <div class="col-2">
                    <p class="mb-0 font-d-reg text-white"><?php echo $game['bet_size']; ?></p>
                </div>
                <div class="col-2">
                    <p class="mb-0 font-d-reg text-white"><?php echo $game['multiplier']; ?></p>
                </div>
                <div class="col-2">
                    <p class="mb-0 font-d-reg text-white"><?php echo $game['payout']; ?></p>
                </div>
            </div>
            <?php
        }
    }
    ?>


    <div class="table-pagination-btn d-flex justify-content-between">
        <div class="left-btn">
            <?php if ($currentPage > 1) { ?>
                <a href="javascript:void(0)" onclick="loadData(<?php echo $currentPage - 1; ?>)"><i class="fa-solid fa-angle-left"></i></a>
            <?php } ?>
        </div>
        <div class="pagination-number">
            <ul class="ps-0 mb-0 d-flex">
                <?php for ($i = 1; $i <= $totalPages; $i++) { ?>
                    <li><a href="javascript:void(0)" class="font-d-bld <?php echo $i == $currentPage ? 'active' : ''; ?>" onclick="loadData(<?php echo $i; ?>)"><?php echo $i; ?></a></li>
                <?php } ?>
            </ul>
        </div>
        <div class="right-btn">
            <?php if ($currentPage < $totalPages) { ?>
                <a href="javascript:void(0)" onclick="loadData(<?php echo $currentPage + 1; ?>)"><i class="fa-solid fa-angle-right"></i></a>
            <?php } ?>
        </div>
    </div>
</div>

This is the API request code:

$response = file_get_contents('https://bht.bet/api/dzWJjyef5PIbA1nMjQPW2KTTy1gfqUiw/hunts/94');
$values = json_decode($response, true);

// create an empty array to store the values
$data = array();

// check if the 'bonuses' key exists
if (isset($values['bonuses'])) {
    // loop through the values and add them to the array
    foreach ($values['bonuses'] as $key => $value) {
        $data[] = array(
            'name' => $value['name'],
            'bet_size' => $value['bet_size'],
            'multiplier' => $value['multiplier'],
            'payout' => $value['payout']
        );
    }
}
0 likes
2 replies
LaryAI's avatar
Level 58

The error "Undefined array key 0" occurs when the $data array is empty and the code tries to access its first element. To fix this, you can add a check to make sure that $data is not empty before trying to access its elements in the for loop. Here's the updated code:

<?php include('api.php');?>

<div class="table-list">
    <?php
    if (empty($data)) {
        // handle empty data
    } else {
        $valuesPerPage = 8;
        $totalPages = ceil(count($data) / $valuesPerPage);

        if (isset($_GET['page'])) {
            $currentPage = $_GET['page'];
        } else {
            $currentPage = 1;
        }

        $startIndex = ($currentPage - 1) * $valuesPerPage;
        $endIndex = $startIndex + $valuesPerPage;

        $count = ($currentPage - 1) * $valuesPerPage + 1;

        if (!empty($data)) {
            for ($i = $startIndex; $i < $endIndex && $i < count($data); $i++) {
                $game = $data[$i];
                ?>
                <div class="row list-item">
                    <div class="col-4">
                        <h4 class="mb-0 font-d-bld text-white"><?php echo $count++; ?>. <?php echo $game['name']; ?></h4>
                    </div>
                    <div class="col-2">
                        <p class="mb-0 font-d-reg text-white"><?php echo $game['bet_size']; ?></p>
                    </div>
                    <div class="col-2">
                        <p class="mb-0 font-d-reg text-white"><?php echo $game['multiplier']; ?></p>
                    </div>
                    <div class="col-2">
                        <p class="mb-0 font-d-reg text-white"><?php echo $game['payout']; ?></p>
                    </div>
                </div>
                <?php
            }
        }
    }
    ?>


    <div class="table-pagination-btn d-flex justify-content-between">
        <div class="left-btn">
            <?php if ($currentPage > 1) { ?>
                <a href="javascript:void(0)" onclick="loadData(<?php echo $currentPage - 1; ?>)"><i class="fa-solid fa-angle-left"></i></a>
            <?php } ?>
        </div>
        <div class="pagination-number">
            <ul class="ps-0 mb-0 d-flex">
                <?php for ($i = 1; $i <= $totalPages; $i++) { ?>
                    <li><a href="javascript:void(0)" class="font-d-bld <?php echo $i == $currentPage ? 'active' : ''; ?>" onclick="loadData(<?php echo $i; ?>)"><?php echo $i; ?></a></li>
                <?php } ?>
            </ul>
        </div>
        <div class="right-btn">
            <?php if ($currentPage < $totalPages) { ?>
                <a href="javascript:void(0)" onclick="loadData(<?php echo $currentPage + 1; ?>)"><i class="fa-solid fa-angle-right"></i></a>
            <?php } ?>
        </div>
    </div>
</div>

In the updated code, we added an if statement to check if $data is not empty before entering the for loop. This will prevent the "Undefined array key 0" error from occurring.

isaacr's avatar

Your code seems to work, here's a repl: https://replit.com/@isaacrnz/PHP-Ajax-test

One thing I would suggest however is to not rely on $data being populated like $game = $data[$i]; Instead you could write your loop like so:

foreach ($data as $i=>$game) {

This would not only reduce the amount of code but it will stop the Undefined array key error you're getting

Please or to participate in this conversation.