blinks Posted November 27, 2009 Share Posted November 27, 2009 I think this is probably a fairly simple question, but I just can't see the solution! I have this bit of code - $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC); foreach ($res as $row) { if ($row[type'] == 3) { //record $pidlist[]['pid'] = $row['pid']; $pidlist[]['date'] = $row['date']; } elseif ($row[type'] == 2){ //collection $interim_pidlist[] = $row['pid']; } } which is yielding the following output for $pidlist- Array ( [0] => Array ( [pid] => changeme:150 ) [1] => Array ( [date] => 2008-07-14 12:02:32 ) [2] => Array ( [pid] => changeme:152 ) [3] => Array ( [date] => 2008-07-14 12:18:17 ) [4] => Array ( [pid] => changeme:65 ) But I want the output to be Array ( [0] => Array ( [pid] => changeme:150 [date] => 2008-07-14 12:02:32 ) [1] => Array ( [pid] => changeme:152 [date] => 2008-07-14 12:18:17 ) ... [/code] I thought the answer would be using such as $pidlist['pid'][], but this yields - Array ( [pid] => Array ( [0] => changeme:150 [1] => changeme:152 ) [date] => Array ( [0] => 2008-07-14 12:02:32 [1] => 2008-07-14 12:18:17... Link to comment https://forums.phpfreaks.com/topic/183097-array-help/ Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 You can get this working by explicitly using a counter, such as $i = 0. $i = 0; $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC); foreach ($res as $row) { if ($row[type'] == 3) { //record $pidlist[$i]['pid'] = $row['pid']; $pidlist[$i]['date'] = $row['date']; } elseif ($row[type'] == 2){ //collection $interim_pidlist[] = $row['pid']; } $i++; } Link to comment https://forums.phpfreaks.com/topic/183097-array-help/#findComment-966307 Share on other sites More sharing options...
blinks Posted November 27, 2009 Author Share Posted November 27, 2009 Thanks for your speedy help; will give this a go on Monday. I did actually try what you suggested, but I realise now that I had set up multiple instances of $pidlist[$i]['pid'] = $row['pid']; etc throughout the code, re-initialising i back to 0 at each instance. Thanks so much! Link to comment https://forums.phpfreaks.com/topic/183097-array-help/#findComment-966310 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.