LanterneRouge
LanterneRouge

Reputation: 199

Basic PHP Array

I have the following Array:

$a = array();
$handle = fopen('test.csv','r');
while (!feof($handle)){
    $a = fgetcsv($handle,",");
}
fclose($handle);
echo $a[100];

Accompanied with the following CSV file (test.csv):

100,1245
500,111
600,12

I am getting the following error:

Notice: Undefined offset: 100

I don't understand what I am doing wrong. I have two columns, I want the first column to be the key, and the second column the value. I would then expect to have $a[100] return 1245. What am I missing? Why is this so hard?

EDIT

I want the first column to be the key and the second column to be the value. HOW do I achieve that goal? THAT is the question. Please don't get side tracked...

Upvotes: 1

Views: 47

Answers (1)

Ronser
Ronser

Reputation: 1875

You can try

$final  =   array();
foreach($a as $val){
    $var    =   explode(',',$val);
    $final[$var[0]] =   $var[1];
}

echo $final[100];

Output: 1245

Upvotes: 3

Related Questions