PHP Array Index Error When Fetching Twitch API Data

I’m getting this error message on my site: Notice: Undefined offset: 1 in…

I have a PHP script that should pull channel data from my database and show Twitch stream information. The problem seems to be in the section where I loop through the API response. Here’s my code:

<?php
defined('_JEXEC') or die('Access denied.');
$channelList = $params->get('channels');
$channelsArray = explode(',', $channelList);
$apiUrl = "https://api.twitch.tv/helix/streams?user_login=";
$activeStreams = array();

foreach($channelsArray as $index => $channel){
    $apiUrl .= "&";
    $apiUrl .= $channel;
}
unset($channel);

$response = file_get_contents($apiUrl, 0, null, null);
$data = json_decode($response, true);

// This is where the error occurs
foreach($channelsArray as $index => $channel){
    $streamUrl = $data[$index]['user']['profile_url'];
    $urlParts = explode('/', $streamUrl);
    $username = end($urlParts);
    $streamTitle = $data[$index]['title'];
    $gameCategory = $data[$index]['game_name'];
    $viewerCount = $data[$index]['viewer_count'];
    $description = $data[$index]['description'];
    displayStream($username, $viewerCount, $description, $gameCategory);
    $activeStreams[] = checkStatus($username);
}

unset($channel);
unset($index);

function displayStream($streamer, $viewers, $description, $category)
{
    if ($category == "Counter-Strike 2")
    {
        $gameIcon = "cs2.jpg";
    }
    else {
        $gameIcon = "live.png";
    }
    
    if ($streamer != null)
    {
        echo '&nbsp;<img src="./modules/mod_streamlist/images/'.$gameIcon.'">';
        echo '<a href="https://www.twitch.tv/'.$streamer.'" target="_blank"> <strong>'.$streamer.'</strong></a>';
        echo '&nbsp;(' .$viewers.') - ';
        echo '<strong>'.$description.'</strong> <br>';
    }
}

function checkStatus($user){
    if($user != null){
        return $user;
    }
    else {
        return null;
    }
}
?>

<?php
foreach ($channelsArray as $i => $channel1) {
    foreach($activeStreams as $j => $channel2){
        if($channel1 == $channel2){
            unset($channelsArray[$i]);
        }
    }
}

$offlineCount = count($channelsArray);
$totalStreams = count($activeStreams);

if ($offlineCount == $totalStreams){
    echo '<strong><center>No streams are currently live!</center></strong>';
}
?>

What could be causing this offset error? Any suggestions would be helpful.

You’re mixing up array indexes. The Twitch API returns data in $data['data'], not directly indexed by your channel positions. Offline channels won’t show up in the response, so $data[0] might not match $channelsArray[0]. Loop through $data['data'] instead and match by username field.

The problem is you’re assuming the API response matches your input array structure. When streams go offline, they just disappear from the response - so your indexing breaks. I hit this same issue building a module for our gaming site. Twitch’s API only returns active streams in the ‘data’ array, so $data[$index] crashes when that index doesn’t exist. Don’t use your channel array indices. Instead, loop through $data['data'] and match streams by ‘user_login’ against your channel names. This handles offline channels without throwing undefined offset errors.

I hit this exact problem building a community dashboard with Twitch’s API. Here’s what’s happening: you’re assuming the API response keeps the same order and count as your input channels, but it doesn’t. The API only returns live streams in $data['data'] - not all channels you asked for. Your code thinks $channelsArray[0] matches $data[0], but if your first channel is offline, $data[0] will be whatever channel happens to be live. You need to loop through $data['data'] and use the user_login field to match each stream to the right channel instead of relying on array positions.

The offset error arises because the API response does not match the indices of your channels array. The Twitch API only provides details for currently live streams within a ‘data’ field, meaning you should first access $data['data']. This array contains only the active channels, and any channels that are not streaming will not be included. I encountered a similar issue when I began working with streaming APIs, as they exclusively return information on active streams. Update your loop to iterate through $data['data'] instead of relying on $channelsArray indices. Additionally, implementing error checks for potential API failures or unexpected data formats would be beneficial.

Yeah, same thing happened to me. You’re trying to access $data[$index] but the Twitch API doesn’t work that way. Check if $data['data'] exists first, then loop through that array instead of using channel indexes.