Two dimensional array problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I want to put the contents of a csv file into a 2 dimensional array so that
I can access any element in a $table[x][y] manner and have come up with the
following code:

$complete = file("source.csv");
foreach ($complete as $value) {
$data = explode(",", $value);
$table[$row] = $data;
echo "$table[6][4]";

However the echo statement returns "Array[4]" instead of the 4th element in
the 6th row.

Any suggestions on what I ought to be doing please to make $table a proper 2
dimensional array?

Re: Two dimensional array problem


    echo $table[6][4];

without double-quotes.

if you need to echo more strings with the $table just "add" it with ".",

    echo "Element ($x,$y): " . $table[$x][$y] . "\n";

Re: Two dimensional array problem

Aha, that's it thanks.

Quoted text here. Click to load it

Re: Two dimensional array problem

btw, $table is a proper 2-dim array, but the code could be enhanced

// first example
$table = array();
$file = fopen("source.csv","r");

// second example
$table = array();
$file = file("source.csv");
foreach($file as $id => $item)
  $table[$id] = explode(",",$item);

both would result in an array $table, where
  -> the 1st dimension defines the line-number, starting with 0
  -> the 2nd dimension defines the position of the value in that line,
     starting with 0

Site Timeline