tomdchi Posted November 8, 2009 Share Posted November 8, 2009 Hi, I am trying to create a csv that in turn is used in report generation. I have the array below that I need to loop with two foreach loops. Can someone please help with me to get the foreach's correct? Thanks! Tom The array Array ( [0] => Array ( [date] => 2009-11-08 [fundingid] => 123789456 [processed] => 1 [amount] => 341.00 [payments] => Array ( [0] => Array ( [id] => 29 [firstname] => Al [lastname] => Allen [userid] => 29 [amountin] => 110.00 [fundingdate] => 2009-11-09 [processed] => 1 [invoiceid] => 2 [fundingid] => 123789456 [pmttotal] => 341.00 ) [1] => Array ( [id] => 26 [firstname] => Robin [lastname] => Zander [userid] => 26 [amountin] => 110.00 [fundingdate] => 2009-11-09 [processed] => 1 [invoiceid] => 3 [fundingid] => 123789456 [pmttotal] => 341.00 ) [2] => Array ( [id] => 16 [firstname] => Tom [lastname] => vaughan [userid] => 16 [amountin] => 121.00 [fundingdate] => 2009-11-09 [processed] => 1 [invoiceid] => 1 [fundingid] => 123789456 [pmttotal] => 341.00 ) ) ) ) Code snippet that I am working on: foreach($deposits as $value=>$field) { $depdate = $field['date']; $transid = $field['fundingid']; $deptotal = $field['amount']; $depstatus = $field ['processed']; foreach($key as $field2) { $name = $field2['firstname'].' '.$field2['lastname']; $fundingid = $field2['fundingid']; $fundingdate = $field2['fundingdate']; $processed = $field2['processed']; $amountin = $field2['amountin']; $invoiceid = $field2['invoiceid']; fwrite($handle, ';'); fwrite($handle, date('m-d-Y', strtotime($fundingdate))); fwrite($handle, ';'); fwrite($handle, ';'); fwrite($handle, $name); fwrite($handle, ';'); fwrite($handle, ';'); fwrite($handle, $invoiceid); fwrite($handle, ';'); fwrite($handle, ';'); fwrite($handle, $fundingid); fwrite($handle, ';'); fwrite($handle, ';'); if ($processed == 1) { fwrite($handle, "Pending"); } elseif ($processed == 2) { fwrite($handle, "Processed"); } elseif ($processed == 3) { fwrite($handle, "Refund Pending"); } elseif ($processed == 4) { fwrite($handle, "Refund Complete"); } elseif ($processed == 5) { fwrite($handle, "Trans Returned"); } elseif ($processed == 6) { fwrite($handle, "Processing"); } elseif ($processed == 7) { fwrite($handle, "Void"); } fwrite($handle, ';'); fwrite($handle, ';'); fwrite($handle, $amountin); fwrite($handle, ';'); fwrite($handle, "\r\n"); } fwrite($handle, "\r\n"); fwrite($handle, "\r\n"); fwrite($handle, "\r\n"); fwrite($handle, "\r\n"); fwrite($handle, ';;'); fwrite($handle, $depdate); fwrite($handle, ';'); fwrite($handle, ';'); fwrite($handle, $transid); fwrite($handle, ';'); fwrite($handle, ';'); fwrite($handle, $deptotal); fwrite($handle, ';'); fwrite($handle, ';'); fwrite($handle, $depstatus); } Link to comment https://forums.phpfreaks.com/topic/180732-solved-hep-with-nested-foreach/ Share on other sites More sharing options...
The Little Guy Posted November 8, 2009 Share Posted November 8, 2009 in your second foreach, where does $key come from? shouldn't it be $value? Link to comment https://forums.phpfreaks.com/topic/180732-solved-hep-with-nested-foreach/#findComment-953524 Share on other sites More sharing options...
sasa Posted November 8, 2009 Share Posted November 8, 2009 insert line $key = $field['payments'];[>/code]before 2nd loop Link to comment https://forums.phpfreaks.com/topic/180732-solved-hep-with-nested-foreach/#findComment-953532 Share on other sites More sharing options...
tomdchi Posted November 8, 2009 Author Share Posted November 8, 2009 Thanks sasa!! Problem Solved!! Link to comment https://forums.phpfreaks.com/topic/180732-solved-hep-with-nested-foreach/#findComment-953559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.