Notice:Undefined offset - php

I've been using this script in the past and was ok. I converted txt file to csv and have got some undefinded error.
function Importcsv($filename) { $row = 0;
$col = 0;
$handle = #fopen($filename, "r");
if ($handle)
{
while (($row = fgetcsv($handle, 4096)) !== false)
{
if (empty($fields))
{
$fields = $row;
continue;
}
foreach ($row as $k=>$value)
{
$results[$col][$fields[$k]] = $value;
}
$col++;
unset($row);
}
if (!feof($handle))
{
echo "Error: unexpected fgets() failn";
}
fclose($handle);
}
return $results; }
$filename = "tm_data.csv";
$csvArray = Importcsv($filename);
foreach ($csvArray as $row)
{
echo $row['CITY'];
}
Please let me know if you have any idea or discovered similar issues before.
Please see few error lines
Notice: Undefined offset: 386 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 387 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 388 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 389 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Notice: Undefined offset: 390 in /Applications/XAMPP/xamppfiles/htdocs/tm_marketing/index.php on line 20
Thanks,

From the (lack of) information posted in the question I think line 20 is:
$results[$col][$fields[$k]] = $value;
It seems that the first row of your CSV file contains less columns than the data rows.
The first row (the column names) is stored in $fields and it has 386 columns. Some of the data rows contain more than 386 columns (391, it seems) and for $k starting with 386, $fields[$k] is undefined and triggers the notices you posted.
The solution is up to you. You can fix the CSV file by adding the missing column names or by removing the extra values from the offending row(s).
You can also modify the code to do one of the above (either generate some column names on-the-fly or ignore the extra values).
You should also check if the CSV file is properly encoded. Maybe there is no extra column and fgetcsv() incorrectly split the row. Check the separator, quote and escape characters used in the file and make sure you use the same values in the call to fgetcsv().

Related

explode() offset and delimiter warnings when delimiter is valid

I'm having a heck of a time trying to figure out what is going on. I have a block of code that is throwing some errors. Outside of the Warnings and Notices the code works fine and the arrays are not empty. But why I am getting those warnings and notices is bothersome. In my test environment I don't get the errors however when put on a different server they pop right up. I am stumped as to why. The lines of code that are throwing the errors and the errors being thrown are as follows
$libTitle = explode($libYearRes[0], $value);
Notice: Undefined offset: 0
Warning: explode(): Empty delimiter
$sortLibYearRes = explode(' ', $libYearRes[0]);
Notice: Undefined offset: 0
$libMovieRes = $sortLibYearRes[1];
Notice: Undefined offset: 1
The entire block of code is as follows with the problem lines denoted with a # in them
function checkMovieLibrary($movieTitle,$movieLibrary){
foreach ($movieLibrary as $value) {
preg_match('/\(\d\d\d\d\)\s\d\d\d*[Pp]/', $value, $libYearRes);
$libTitle = #explode($libYearRes[0], $value);
$libMovieTitle = rtrim($libTitle[0]);
$sortLibYearRes = #explode(' ', $libYearRes[0]);
//list($libMovieYear,$libMovieRes) = explode(' ', $libYearRes[0], 2);
$libMovieYear = $sortLibYearRes[0];
$libMovieRes = #$sortLibYearRes[1];
preg_match('/\(\d\d\d\d\)\s\d\d\d*[Pp]/', $movieTitle, $newfileYearRes);
$newFileTitle_array = explode($newfileYearRes[0], $movieTitle);
$newFileTitle = rtrim($newFileTitle_array[0]);
$sortFileYearRes = explode(' ', $newfileYearRes[0]);
$newFileMovieYear = $sortFileYearRes[0];
$newFileMovieRes = $sortFileYearRes[1];
if(stripos($libMovieTitle.' '.$libMovieYear, $newFileTitle.' '.$newFileMovieYear) !== false){
$moviesList_array[] = array('OriginalFile' => $value, 'NewFile' => $movieTitle);
return($moviesList_array);
}
}
return false;
}
//$movieTitle = "10 Cloverfield Lane (2016) 1080p.mkv"; //This is an example of $movieTitle string
//$movieLibrary = array('10 Cloverfield Lane (2016) 1080p.mkv','The Goonies (1985) 1080p.mp4'); this is an example of the $movieLibrary array
$doesMovieExist = checkMovieLibrary($Movie,$movieLibrary);
if(is_array($doesMovieExist) && $doesMovieExist !== false){
foreach($doesMovieExist as $fileKey => $fileValue) {
$originalFile = $fileValue['OriginalFile'];
$newFile = $fileValue['NewFile'];
}
//do some stuff here
}
First of all dont use # because it will bypass the error message. So user error_reporting(E_ALL) till development phase.
Then you are getting undefined offset it means your array is empty of does not exist so put a condition to check if it exist then the explode should work. It must be like that :
if(!empty($libYearRes[0]) || isset($libYearRes[0])) {
$libTitle = #explode($libYearRes[0], $value);
}
Undefined offset error means you're referring an array key that does not exist. If you like to see the array before processing it, use variable dump (var_dump) then you will see these array keys are not available for the given scenario.
you can avoid this error by checking array key really exist or not by using isset function. as example
if(isset($libYearRes[0])){ // will be true only if $libYearRes[0] is exist
$libTitle = explode($libYearRes[0], $value);
}

PHP foreach over an array twice results in some warnings undefined index

I have the following php code that is looking for the last entry of test entry in a mysql query result.
It checks if the last entry is valid for a device or it tries to search the latest one in the for that type of test (or failing that leave it as untested). After that, it does the same for the second device in the same manner. However I get errors pointing to lines on second foreach loop.
if ($device1_valid) {
$Results_d1 = $History[$TestNo][$iter]['Results_d1'];
$Colour_d1 = Colour($Results_d1);
$Date_d1 = $History[$TestNo][$iter]['Date_'];
} else {
foreach ($History[$TestNo]['iter'] as $item) {
$device1_valid = $History[$TestNo][$item]['d1_valid'];
if ($sf1_valid) {
$Results_d1 = $History[$TestNo][$item]['Results_d1'];
$Colour_d1 = Colour($Results_d1);
$Date_d1 = $History[$TestNo][$item]['Date_'];
break;
} else {
$Results_d1 = "----";
$DateTime_d1 ="----";
$Colour_d1 = 'white';
}
}
}
unset($item);
if ($device2_valid) {
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Colour_d2 = Colour($Results_d2);
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Date_d2 = $History[$TestNo][$iter]['Date_'];
} else {
foreach ($History[$TestNo]['EntryNo'] as $item) {
$device2_valid = $History[$TestNo][$item]['d2_valid'];
if ($device2_valid) {
$Results_d2 = $History[$TestNo][$item]['Results_d2'];
$Colour_d2 = Colour($Results_d2);
$Date_d2 = $History[$TestNo][$item]['Date_'];
break;
} else {
$Results_d2 = "----";
$DateTime_d2 ="----";
$Colour_d2 = 'white';
}
}
This results in warnings for the second loop as such:
Notice: Undefined index: EntryNo in /server/filename.php on line 129
Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129
Why is this error occurring and how will I be able to remove it? The query does result in correct data (which is displayed later but I don't understand why theses notifications and warning are happening. This only happens in the second foreach loop and not the first.
Edit:
$History[$TestNo] is a multidimensional array.... so vardump gives array(49) { [0]=> array(25) {....} [1]=> array(25) [2]=> array(25){...} etc. I call this function setting $EntryNo to 0.
vardump $History[$TestNo][$EntryNo] simply gives array(25) {....}
There are no warnings in the first loop but second loop it says the index is undefined. This is key reason why the other question identified as duplicate does not address my issue. The question is why is this occuring in the second foreach loop and how can I avoid this.
For
`Notice: Undefined index: EntryNo in /server/filename.php on line 129
Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129'
It must be like this at foreach($History[$TestNo]['EntryNo'] as $item) There is no element in array $History[$TestNo] with with key EntryNo.
Would you please var_dump($History[$TestNo]) and check it ?
Notice: Undefined variable: Colour_sf2 in /server/filename.php on line 184
For this, You have not included enough code here but it must be because you have not defined $Colour_sf2 before use it in any function or condition.

Get a specific line from object (php)

i get some URLS from HTML site by using
foreach($html->find('source') as $video)
if($video->type =='video/mp4') {
echo $video->src. '<br>';
my output looks like :
http://video.csfd.cz/321/321909/130228151/360.mp4
http://video.csfd.cz/321/321909/130228151/720.mp4
http://video.csfd.cz/321/321909/99476124/360.mp4
http://video.csfd.cz/321/321909/99476124/720.mp4
http://video.csfd.cz/321/321909/99476124/1080.mp4
and i have just no idea how to get just one of this links, i dont know much about objects so it looks pretty impossible for me to solve this problem.
What i try:
I was thinking about converting object to array and work with it, which sound pretty easy, problem is that if i use :
$pole = (array)$video;
echo "$pole[0]";
it says:
http://video.csfd.cz/321/321909/130228151/360.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/130228151/720.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/99476124/360.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/99476124/720.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/99476124/1080.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
if i use print_r the output is like tons of code , atleast for 1-2x A4 page, soemthing really crazy
So is there a way how to get one specific line from this (prefer to let me chose which one) , or some way to convert this output to array or anything like that?
It is a bad idea to convert objects into arrays. You could basicly store the links in an array while looping. Try this :
$links = array();
foreach ($html->find('source') as $video) {
if ($video->type == 'video/mp4') {
array_push($links, $video->src);
}
}
And then just use the $links array however you want :
echo $links[0];

Uninitialized string offset while building array

I am getting the following error from the method presented below:
Notice: Uninitialized string offset: 5 in /path/to/file.php on line 30 Fatal error: Cannot access empty property in path/to/file.php on line 30
private function parse($xml, $index = '') {
echo count($xml->children()); //outputs 6
$count = 0;
foreach ($xml->children() as $key => $value) {
$this->$key[$count] = array();
$count++;
}
}
Any ideas why if I build an multi-dimensional in this way it results in an error?
If I change the assignment to:
$this->$key = array($count = > array());
This simply re-assigns the property each loop.
Thanks
Rich
Imagine you've got a string:
$string = 'abc`;
Doing substring access (which looks like array) will return you the character:
echo $string[2]; # c
Or you get your error when you're out of the index:
echo $string[3]; # null + warning
So now accessing a member of your object $this dynamically:
$this->$string[2]; # access $this->c
However this one breaks hardly:
$this->$string[3]; # access $this->null (not possible)
This gives you your fatal error of an empty property, a property with no name.
This explain what happens in your code, you have not told what you're trying to do so I hope this information will help you to continue with writing your parse function.
You should try to create the array before filling it.
I.e. $this->key = array();
That is, before looping through the XML elements.

Undefined variable, but it is there

I've got an array, but when I try to use it, I get the Undefined variable notice.
Here's the relevant lines:
$varEvents = array();
...
if ($selectedResult) {
while ($row = mysql_fetch_assoc($selectedResult)) {
array_push($varEvents, $row['eventID']);
}
mysql_free_result($selectedResult);
}
...
print_r($varEvents);
if (is_array($varEvents)) {
if (count($varEvents) > 0) {
if (in_array($id, $varEvents)) {
$varRegistered = 1;
}
}
unset($varEvents);
}
and the result shows as:
Array ( [0] => 4 )
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 143
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 145
line 143: print_r($varEvents);
line 145: if (is_array($varEvents)) {
All relevant lines are in the same loop and I do get most of the results I expect, except $varRegistered never changes to 1 and that messes up my result.
It is most likely because of this line:
unset($varEvents);
You are unsetting the variable within the loop and next iterations don't find it again.

Categories