Undefined offset: 2 and Undefined offset: 1 and Uninitialized string offset: 0 - php

getting the notice error for array how to resolve this unintialized string and undefined offset.
for($i=0;$i<count($exp_company);$i++)
{
$exp_fd2 = explode("/",$exp_fd1 [$i]);
$exp_td2 = explode("/",$exp_td1 [$i]);
$exp_fd = $exp_fd2[2]."-".$exp_fd2[1]."-".$exp_fd2[0];
$exp_td = $exp_td2[2]."-".$exp_td2[1]."-".$exp_td2[0];
mysql_query("insert into empexp(employee_id,company_name,designation,from_date,to_date,exp_description)values('$insert_id','$exp_company[$i]','$exp_designation[$i]','$exp_fd','$exp_td','$exp_description[$i]')");
}

Here You are Trying to Access the index which does not exist.
undefined offset means that an array has run out of bounds as simple as that.
let's assume your array is:
[0]==> 'a',
[1]==> 'b',
From above I cannot access array[2] as it does not exist.
the array size is smaller than the index that you are trying to fetch.

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.

How to make array element to string using php

I have array element like this,
$array = array(rha3pf, rha3pf, adfsasdf, asdfsad);
I want to make a unique, for that i used,
$uniqe = array_unique($array);
print_r($uniqe);
I want result as,
$array = array(rha3pf, adfsasdf, asdfsad);
I am getting result as,
E_NOTICE : type 8 -- Use of undefined constant rha3pf - assumed 'rha3pf' -- at line 3
E_NOTICE : type 8 -- Use of undefined constant rha3pf - assumed 'rha3pf' -- at line 3
E_NOTICE : type 8 -- Use of undefined constant adfsasdf - assumed 'adfsasdf' -- at line 3
E_NOTICE : type 8 -- Use of undefined constant asdfsad - assumed 'asdfsad' -- at line 3
Array ( [0] => rha3pf [2] => adfsasdf [3] => asdfsad )
I am not getting correct answer in live. How to solve this
Thanks in advances
The following code
$array = array('rha3pf', 'rha3pf', 'adfsasdf', 'asdfsad');
$a = implode(" ",$array);
$a = array_unique($a);
print_r($a);
will print
rha3pf adfsasdf asdfsad
What is $a in print??
<?php
$array = array(rha3pf, rha3pf, adfsasdf, asdfsad);
$uniqe = array_unique($array);
print_r($uniqe);
//^^^ this part
?>
Working demo here

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] = "";
}

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

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.

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