-1

I get the error:

Notice: Undefined index: marca

When I'm trying to do this:

echo $data[0]['marca'];

But if I print the array, like this:

print_r($data[0]);

The output is:

Array ( [marca] => Jack&co [stock] => 10 [nome] => JW0114M2 [codice] => JW0114M2 [caratteristiche] => JW0114M2 Classe articolo: orologio da polso Sesso: unisex Movimento: quarzo Bracciale: acciaio Tipologia: cronografo [prezzo al pubblico €] => 99,00 [sconto %] => 75 [prezzo orologistock €] => 25,00 [img] => http://orologistock.it/virtual_img/ )
       //^^^^^ See the element exists!

So why can't I access the array element?

EDIT:

The output from the source code with var_dump($data[0]); is:

array(9) { ["marca"]=> string(7) "Jack&co" ["stock"]=> string(2) "10" ["nome"]=> string(8) "JW0114M2" ["codice"]=> string(8) "JW0114M2" ["caratteristiche"]=> string(117) "JW0114M2 Classe articolo: orologio da polso Sesso: unisex Movimento: quarzo Bracciale: acciaio Tipologia: cronografo " ["prezzo al pubblico €"]=> string(5) "99,00" ["sconto %"]=> string(2) "75" ["prezzo orologistock €"]=> string(5) "25,00" ["img"]=> string(35) "http://orologistock.it/virtual_img/" } 

I'm getting my array from a .csv file:

function csv_to_array($filename='', $delimiter=';')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header){
                $header = $row;
            }
            else{
                if(count($row) == 9)
                    $data[] = array_combine($header, $row);
            }
        }
        fclose($handle);
    }
    return $data;
}


$data= csv_to_array($_FILES['data']['tmp_name']);
hhz
  • 116
  • 7
  • 1
    1. What is the **exact** output if you take it from the source code, when you do: `var_dump($data[0]);` 2. How do you create this array? – Rizier123 May 01 '15 at 10:17
  • Please, please use the [edit](http://stackoverflow.com/posts/29985096/edit) button and add the output there, don't post code in comments! The edit button is there for a reason and you are free to use it – Rizier123 May 01 '15 at 10:23
  • If this is a single dimension array with only one entry ( which is how it looks ) then surely simply $data['marca'] would suffice? – Professor Abronsius May 01 '15 at 10:24
  • @RamRaider Did you even read the question? `print_r($data[0]);` <- If this prints an array it's at least 2 dimensional! – Rizier123 May 01 '15 at 10:26
  • @Cooper *2. I read out a uploaded csv file* <- Please show the code how you read it and also which encoding does the csv file have? – Rizier123 May 01 '15 at 10:29
  • check with any other key of your array and see if it displays the data properly if so then your key marca may be containing some special characters. – Suchit kumar May 01 '15 at 10:33
  • May be it is stupid, but if your natural language isn't English, but some of letters in 'marca' is the same on your natural language and in English, check which one you've used. Main natural language is Bulgarian ( cirillic ) and I've got some situations as described. – Kancho Iliev May 01 '15 at 10:34
  • i guess your key marca on print_r shows something as:marca but other keys are working like stocks – Suchit kumar May 01 '15 at 10:36
  • i dont know which encoding the file have, but i saved it as utf-8 and everything works fine, so its the encoding, thanks for your fast help everyone :) – hhz May 01 '15 at 10:46

1 Answers1

0

Encoding of file was wrong, change encoding to UTF-8 solved the Problem.

hhz
  • 116
  • 7
  • Thanks, just wanted to accept this answer but there is an error that i can accept my own answer in 2 days :/ – hhz May 01 '15 at 11:09
  • That's not an an error @Cooper, that is how the system works when accepting your own answer. – Jay Blanchard May 01 '15 at 12:06