PHP Notice: Undefined Offset: 1 with Array When Reading Data

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.

Notice: Undefined offset: 1 when creating a Array-Element?

. is used for concatenation not for array assignment.So remove it

So the code must be:-

$tabCount = 0;
$mainDivs = array();
if(isset($output) && count($output)>0){ // Check that your array is set and have values so that foreach will not produce error
foreach($output as $node) {
$mainDivs[$tabCount] = 'blablabla';
$tabCount++;
}
}

Note:-

Reference:-

String concatenation:- http://php.net/manual/en/language.operators.string.php

Array:- http://php.net/manual/en/language.types.array.php

Undefined offset error in array index

If you check for existance of an array element by using $array[1] == null , php will throw a Notice: Undefined offset: 1 , you should use !isset($array[1]) instead. Otherwise, your code contains no errors.

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

Exploding a line from a file gives Undefined Offset error in PHP

A quick fix, would be to check that you have a line with 7 elements, so after your explode, just check how many elements the array has and skip the rest of the processing if there aren't enough values...

$pieces = explode(",", $line);
if ( count($pieces) != 7) {
continue;
}
$date = $pieces[0];
// ....

You should also look into using prepared statements as the offer more security, although you will still have to do a substitution for the table name as you do now (you can't have a table name as a bind variable).

Also as a recommendation, when doing an INSERT,always list the column names ...

INSERT INTO $tableName (date, open, ... )
VALUES ('$date', '$open', ...)

as this ensures that it's clear which column is where and if any table changes are made that the INSERT is explicit about which values it is using for which columns.

Notice: Undefined offset: 1 in file.php on line 33

The Undefined offset error means that an array item in that varible does not exist. This suggests that the issue is futher up in your code where the array should be created (but is not). For example, the preg_split("/[\s,]+/",$line); line is probably the issue here.

To find out add this line:

var_dump($piece_array);

after this line

$piece_array = preg_split("/[\s,]+/",$line);

If you need futher help post the outcome as an edit and I will try to help you.

PHP Notice: Undefined offset: 1 with string of date field

You wan't to work with Date formats- Your problem is, you split the Date simply with explode - This way is not correct, because your splitted data has following format:

[0] "2014"
[1] "07"
[2] "21"

You check the splitted string (after splitting, each entry is also an string to) with integers:

if("07" == 1) {

The second problem is, you don't validate, if the data is set. The next problem is: You want to display only another date format - Please use PHP-based functions for formatting. Here are a sample for you:

<?php
$press_date = '2014-07-21';

print date('d. M Y', strtotime($press_date));

Output: 21. Jul 2014

<?php
$press_date = '2014-07-21';

print date('l, d. F Y', strtotime($press_date));

Output: Monday, 21. July 2014

Informations about the output format can be found at http://php.net/manual/de/function.date.php.



Related Topics



Leave a reply



Submit