Notice: Undefined offset: 0 using Array in [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
Not sure how to fix this error
Notice: Undefined offset: 0 in C:\xampp\htdocs\streams.php on line 50
Notice: Undefined offset: 0 in C:\xampp\htdocs\streams.php on line 53
Notice: Undefined offset: 0 in C:\xampp\htdocs\streams.php on line 54
Code its referring to:
<?php
$members = array("hawkmyg");
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
//grabs the channel data from twitch.tv streams
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
//get's member names from stream url's and checks for online members
foreach($members as $i =>$value){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
Cannot figure out how to fix

The notice of an undefined offset occurs when one calls an array element with the specific index, for example, echo $array[$index], but the index is not defined within the array.
In your code, the array $members has one element (with index 0). So we're walking exactly one time through your foreach loop.
You're calling $json_array[$i]['channel']['channel_url'] where $i = 0, but $json_array[0] does not exist.
You should check the contents of $json_array using print_r() or var_dump().
I tested the script myself, and when I read the contents of the link http://api.justin.tv/api/stream/list.json?channel=,hawkmyg, it returned an empty JSON array. The channel 'hawkmyg' does not exist.
I tried the channel 'hatoyatv', and it just worked.

Related

Php Dynamic Variable

$X['high'] = 1234;
$var = array("X","high");
This is working:
$temp = $$var[0];
$temp = $temp[$var[1]];
echo $temp;
But this isn't working:
echo $$var[0][$var[1]];
Why? How can i make it works?
You should explain to php parser how you want this statement to be parsed:
echo ${$var[0]}[$var[1]];
Without brackets you will have:
php7
Notice: Array to string conversion in /in/cvZqc on line 5
Notice: Undefined variable: Array in /in/cvZqc on line 5
php5
Warning: Illegal string offset 'high' in /in/cvZqc on line 5
Notice: Array to string conversion in /in/cvZqc on line 5
Sample link.

Notice: Undefined offset: 1 ERROR

I get this error, but the code works correctly.
Notice: Undefined offset: 1
Here is the php:
$surl = substr($_SERVER['REQUEST_URI'], 1);
$params = explode('/test.php?=', $_SERVER['REQUEST_URI']);
foreach ($params AS $key => $value) {$$key = $value;}
if ($_SERVER['REQUEST_URI']=='/test.php?='){
$link=$params[1];
}else{
$link=$params[1];
}
I don't think you've posted the whole stack trace, but it seems your $params array doesn't have 2 indices in it, which leads me to believe $_SERVER['REQUEST_URI'] does not include '/test.php?='. Print the request uri to see what it does contain, and explode using that as the delimiter.

PHP notices - undefined [duplicate]

I am receiving the following error in PHP
Notice undefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36
Here is the PHP code that causes it:
<?php
# ...
function get_match($regex, $content)
{
preg_match($regex,$content,$matches);
return $matches[1]; // ERROR HAPPENS HERE
}
What does the error mean?
If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:
function get_match($regex,$content)
{
if (preg_match($regex,$content,$matches)) {
return $matches[0];
} else {
return null;
}
}
How to reproduce this error in PHP:
Create an empty array and ask for the value given a key like this:
php> $foobar = array();
php> echo gettype($foobar);
array
php> echo $foobar[0];
PHP Notice: Undefined offset: 0 in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) :
eval()'d code on line 1
What happened?
You asked an array to give you the value given a key that it does not contain. It will give you the value NULL then put the above error in the errorlog.
It looked for your key in the array, and found undefined.
How to make the error not happen?
Ask if the key exists first before you go asking for its value.
php> echo array_key_exists(0, $foobar) == false;
1
If the key exists, then get the value, if it doesn't exist, no need to query for its value.
Undefined offset error in PHP is Like 'ArrayIndexOutOfBoundException' in Java.
example:
<?php
$arr=array('Hello','world');//(0=>Hello,1=>world)
echo $arr[2];
?>
error: Undefined offset 2
It means you're referring to an array key that doesn't exist. "Offset"
refers to the integer key of a numeric array, and "index" refers to the
string key of an associative array.
Undefined offset means there's an empty array key for example:
$a = array('Felix','Jon','Java');
// This will result in an "Undefined offset" because the size of the array
// is three (3), thus, 0,1,2 without 3
echo $a[3];
You can solve the problem using a loop (while):
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
// Increase count by 1, thus, $i=1
$i++;
$groupname[$i] = base64_decode(base64_decode($row['groupname']));
// Set the first position of the array to null or empty
$groupname[0] = "";
}

PHP: Array to string conversion

We're running PHP 5.3.10-1ubuntu3.15 with Suhosin-Patch, and I just ran across the weirdest thing. I keep getting an Array to string conversion error.
Here is some code with line numbers:
115 $report['report'][$key]['report'] = array();
116 watchdog('ranking_report_field', 'key is a: ' . gettype($key), array(), WATCHDOG_NOTICE);
117 $report['report'][$key]['report'] = array(
'#markup' => "<p>No information available.</p><p>For questions, <a href='mailto:$emailAddr'>email</a> your account executive ($emailAddr).</p>",
);
Here are Drupal's (sequential) logs for those line numbers:
Notice: Array to string conversion in foo() (line 115 of /var/www/...
key is a: string
Notice: Array to string conversion in foo() (line 117 of /var/www/...
So far as I can tell there's no array to string conversion that should be taking place. Someone help me out with a second pair of eyes, please - or is this some kind of bug that just hit PHP?
One of the array keys is mapped to a string not an array. Here is a program for how such an error could occur.
<?php
$key = 0;
$report = array();
$report['report'] = array();
$report['report'][$key] = 'report';
// Array to string conversion error
$report['report'][$key]['report'] = array();
// what I assume you are expecting is
$report['report'][$key] = array();
$report['report'][$key]['report'] = array(); // no more notices
NOTE: at his time the OP has not included info for how the array is created

undefined offset PHP error

I am receiving the following error in PHP
Notice undefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36
Here is the PHP code that causes it:
<?php
# ...
function get_match($regex, $content)
{
preg_match($regex,$content,$matches);
return $matches[1]; // ERROR HAPPENS HERE
}
What does the error mean?
If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:
function get_match($regex,$content)
{
if (preg_match($regex,$content,$matches)) {
return $matches[0];
} else {
return null;
}
}
How to reproduce this error in PHP:
Create an empty array and ask for the value given a key like this:
php> $foobar = array();
php> echo gettype($foobar);
array
php> echo $foobar[0];
PHP Notice: Undefined offset: 0 in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) :
eval()'d code on line 1
What happened?
You asked an array to give you the value given a key that it does not contain. It will give you the value NULL then put the above error in the errorlog.
It looked for your key in the array, and found undefined.
How to make the error not happen?
Ask if the key exists first before you go asking for its value.
php> echo array_key_exists(0, $foobar) == false;
1
If the key exists, then get the value, if it doesn't exist, no need to query for its value.
Undefined offset error in PHP is Like 'ArrayIndexOutOfBoundException' in Java.
example:
<?php
$arr=array('Hello','world');//(0=>Hello,1=>world)
echo $arr[2];
?>
error: Undefined offset 2
It means you're referring to an array key that doesn't exist. "Offset"
refers to the integer key of a numeric array, and "index" refers to the
string key of an associative array.
Undefined offset means there's an empty array key for example:
$a = array('Felix','Jon','Java');
// This will result in an "Undefined offset" because the size of the array
// is three (3), thus, 0,1,2 without 3
echo $a[3];
You can solve the problem using a loop (while):
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
// Increase count by 1, thus, $i=1
$i++;
$groupname[$i] = base64_decode(base64_decode($row['groupname']));
// Set the first position of the array to null or empty
$groupname[0] = "";
}

Categories