By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,721 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,721 IT Pros & Developers. It's quick & easy.

Two dimensional array problem

P: n/a
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");
$row=0;
foreach ($complete as $value) {
$data = explode(",", $value);
$table[$row] = $data;
$row++;
}
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?

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
try

echo $table[6][4];

without double-quotes.
if you need to echo more strings with the $table just "add" it with ".",
like:

echo "Element ($x,$y): " . $table[$x][$y] . "\n";
Jul 17 '05 #2

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

// first example
$table = array();
$file = fopen("source.csv","r");
while(!feof($file))
array_push($table,fgetcsv($file,4096));

// 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
Jul 17 '05 #3

P: n/a
Aha, that's it thanks.
"Christopher-Robin" <Ch***************@gmx.de> wrote in message
news:cj*************@news.t-online.com...
try

echo $table[6][4];

without double-quotes.
if you need to echo more strings with the $table just "add" it with ".",
like:

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


Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.