Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Undefined index even if it exists

Tags:

php

It drives me crazy ... I try to parse a csv file and there is a very strange behavior.

Here is the csv

action;id;nom;sites;heures;jours
i;;"un nom a la con";1200|128;;1|1|1|1|1|1|1

Now the php code

$required_fields = array('id','nom','sites','heures','jours');
if (($handle = fopen($filename, "r")) !== FALSE)
{
    $cols = 0;
    while (($row = fgetcsv($handle, 1000, ";")) !== FALSE)
    {
        $row = array_map('trim',$row);
        // Identify headers
        if(!isset($headers))
        {
            $cols = count($row);
            for($i=0;$i<$cols;$i++) $headers[strtolower($row[$i])] = $i;
            foreach($required_fields as $val) if(!isset($headers[$val])) break 2;
            $headers = array_flip($headers);
            print_r($headers);
        }
        elseif(count($row) >= 4)
        {
            $temp = array();
            for($i=0;$i<$cols;$i++)
            {
                if(isset($headers[$i]))
                {
                    $temp[$headers[$i]] = $row[$i];
                }
            }
            print_r($temp);
            print_r($temp['action']);
            var_dump(array_key_exists('action',$temp));
            die();
        }
    }
}

And the output

Array
(
    [0] => action
    [1] => id
    [2] => nom
    [3] => sites
    [4] => heures
    [5] => jours
)
Array
(
    [action] => i
    [id] => 
    [nom] => un nom a la con
    [sites] => 1200|128
    [heures] => 
    [jours] => 1|1|1|1|1|1|1
)
<b>Notice</b>:  Undefined index: action in <b>index.php</b> on line <b>110</b>
bool(false)

The key "action" exists in $temp but $temp['action'] returns Undefined and array_key_exists returns false. I've tried with a different key name, but still the same. And absolutely no problem with the others keys.

What's wrong with this ?

PS: line 110 is the print_r($temp['action']);

EDIT 1

If i add another empty field in the csv at the begining of each line, action display correctly

;action;id;nom;sites;heures;jours
;i;;"un nom a la con";1200|128;;1|1|1|1|1|1|1
like image 310
Spin0us Avatar asked Mar 20 '14 10:03

Spin0us


People also ask

How do I fix Undefined index in PHP?

To resolve undefined index error, we make use of a function called isset() function in PHP. To ignore the undefined index error, we update the option error_reporting to ~E_NOTICE to disable the notice reporting.

Why does PHP show Undefined index?

Undefined Index in PHP is a Notice generated by the language. The simplest way to ignore such a notice is to ask PHP to stop generating such notices. You can either add a small line of code at the top of the PHP page or edit the field error_reporting in the php. ini file.

What does Undefined index mean?

Undefined index mean that the entry was not found in the array. In this case, entries yy , mm and dd doesn't exist in the $_POST array. You have to check if they exists using the PHP language construct command isset.


1 Answers

Probably there is some special character at the beginning of the first line and trim isn't removing it.

Try to remove every non-word character this way:

// Identify headers
if(!isset($headers))
{
    for($i=0;$i<$cols;$i++)
    {
        $headers[preg_replace("/[^\w\d]/","",strtolower($row[$i]))] = $i;
....
like image 51
ojovirtual Avatar answered Sep 19 '22 12:09

ojovirtual