...

View Full Version : loop turning output into double array



itxtme
08-19-2012, 12:16 AM
I am trying to output each row as an array to then export a CSV. I have the code working however it is outputting two of each column value. I even know why it is outputting two, because the array created is listing the value as both indexed and numbered arrays. I dont know why it is doing this!


// Get profiles that were selected
$result_2 = mysql_query("SELECT {$query_ready} FROM all_child LEFT JOIN {$sql_instance}child ON {$sql_instance}child.{$sql_instance}child_id = all_child.child_id") or die(mysql_error());
while($row = mysql_fetch_array($result_2)){
$loop[] = $row;
}



foreach( $loop as $row )
{
fputcsv($outstream, $row, ',', '"');
}

fclose($outstream); ?>


With a print_r I can see the problem,


Array ( [0] => Array ( [0] => Array ( [0] => 1000001 [account_id] => 1000001 [1] => 10000007 [child_id] => 10000007

As you can see account_id is both [0] and [account_id] which is outputting two account ID values (and every other value) in my CSV file! Any help much appreciated

AndrewGSW
08-19-2012, 01:46 AM
mysql_fetch_array fetches as both a numeric and associative array. Try mysql_fetch_row which uses a numeric (enumerated) array.

I'm curious why so many people are still using mysql rather than the (recommended) mysqli? Perhaps there is so much legacy code out there?!

itxtme
08-19-2012, 05:05 AM
Fantastic, that worked like a charm, and seems obvious now that I look back at it! I think you are right, I learnt the original and have stuck to it. I will however take a look at the newer mysqli



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum