Undefined Offset PHP Error

undefined offset PHP error

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;
}
}

Array check undefined offset php

First of all, it doesn't throw an error. It gives you a warning about a possible bug in your code.

if($arrayTime[$i]==""){}

This attempts to access $arrayTime[$i] to retrieve a value to compare against your empty string.

The attempt to read and use a non-existing array index to get a value for comparison is the reason why it throws the warning as this is usually unexpected. When the key does not exist null is used instead and the code continues executing.

if(null == ""){} // this evaluates to true.

Because you are comparing against an empty string "", your answer would be empty():

if(empty($arrayTime[$i])){}

It means you are expecting a key not to exist and at the same time you are checking the value for emptyness. See the type comparison table to see what is and what is not considered 'empty'.

The same rules apply to isset() and is_null(), it wont throw the notice if the key does not exist. So choose the function that best serves your needs.

Keep in mind that by using any of these functions you are checking the value and not if the key exists in the array. You can use array_key_exists() for that.

if(array_key_exists($i, $arrayTime)){}

Error like Undefined offset: 3, issue in project?

The error "Undefined Offset" means you are trying to access an index of an array that does not exist.

My best guess is that the offending code is:

if($sites2[$i] == $s_id){
echo "checked";
}

And an Undefined offset of 3 is telling you that $sites2[3] does not exist.
Instead, you could try something like:

if(isset($sites2[$i]) && $sites2[$i] == $s_id){
echo "checked";
}

Undefined offset PHP error 1

This happens, as RiggsFolly alluded to, when you are trying to access an array by key that does not exist. When your number_format does not return thousands and there is no comma sign, there will be only a single item in the array.

A simple fix would be to guard against by checking if the key exists:

$x_display = array_key_exists(1, $x_array) ? $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '') : $x_array[0]   ;
$x_display .= array_key_exists($x_count_parts - 1, $x_parts) ? $x_parts[$x_count_parts - 1] : '';

Undefined Offset Error on Previous/Next Array

If the problem is that you are accessing a position that does not exist you could try array_key_exists():

if (array_key_exists($currentPage - 1, $totalNodes)) {
// the previous index exist
}

if (array_key_exists($currentPage + 1, $totalNodes)) {
// the next index exist
}

There is a similar question here.

How to fix Undefined offset-Error in PHP

Please see that this is an associative array, where the array keys are "test 1" and "test 2".

If you want to print the first array element, you can write:

echo $testarray['test1'];

PHP Notice: Undefined offset: 1 with array when reading data

Change

$data[$parts[0]] = $parts[1];

to

if ( ! isset($parts[1])) {
$parts[1] = null;
}

$data[$parts[0]] = $parts[1];

or simply:

$data[$parts[0]] = isset($parts[1]) ? $parts[1] : null;

Not every line of your file has a colon in it and therefore explode on it returns an array of size 1.

According to php.net possible return values from explode:

Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

PHP - Notice Undefined offset

I suspect this is the line that's causing the error:

$outputData = array($dataFromTestFile[0], $dataFromTestFile[7]);

You are trying to use array elements at specific index without checking if they actually exists.

Also, you are trying to write an array object to the result file, did you mean to create a comma separated value in that file?

Try this:

$source = '/opt/lampp/htdocs/ngs/tmp/testfile.txt';
$result = '/opt/lampp/htdocs/ngs/tmp/result.txt';

if (($handle = fopen($source, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if (isset($data[0]) && isset($data[7])) {
file_put_contents($result, $data[0] .','. $data[7] ."\r\n");
}
}
fclose($handle);
}

Alternatively, you could write the result as a csv like this also:

$sourceFile = '/opt/lampp/htdocs/ngs/tmp/testfile.txt';
$resultFile = '/opt/lampp/htdocs/ngs/tmp/result.txt';
$resultData = array();

// Parse source file
if (($handle = fopen($sourceFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if (isset($data[0]) && isset($data[7])) {
$resultData[] = array($data[0], $data[7]);
}
}
fclose($handle);
}

// Write result file
if (sizeof($resultData)) {
$h = @fopen($resultFile, 'w');
if (!$h) {
exit('Failed to open result file for writing.');
}
foreach ($resultData as $resultRow) {
fputcsv($h, $resultRow, ',', '"');
}
fclose($h);
}


Related Topics



Leave a reply



Submit