NewBob Posted April 25, 2007 Share Posted April 25, 2007 Hi all! My problem is that when I use 2 nested foreach loops on a 2d array and echo the contents of the inner loop I get doulbe output. Let me demonstrate: $posts = $database->getCalPostByUser('bob'); //returns array[id][row] where id is an int and row is another array foreach($posts as $id => $row) // $row should contain an array { foreach($row as $field => $info) //get the elements in the array $row { echo $field.': '.$info; b(); } } This should work because when I skipped the inner forloop and echoed $row the output was Array. Anywho, the output from the code above is: 0: 20070423 date: 20070423 1: bob username: bob 2: 09:30 start: 09:30 3: 12:00 end: 12:00 4: home location: home 5: stuff and junk description: stuff and junk 6: 1177488289 id: 1177488289 Why do i get the numeric keys 0-6? I have only ever used the string keys. And how can I get rid of the numeric key outputs? I just can't figure it out and I didn't find much on the interweb either. Thanx in advance! /Bob Quote Link to comment https://forums.phpfreaks.com/topic/48597-solved-foreaching-a-2d-array/ Share on other sites More sharing options...
taith Posted April 25, 2007 Share Posted April 25, 2007 the mysql_get_array(); function returns both the associtive names and numbers... when you use it as a while($row=mysql_fetch_array()) it works fine, it only does the associtive row names, else, you have to change your function to mysql_fetch_assoc(); Quote Link to comment https://forums.phpfreaks.com/topic/48597-solved-foreaching-a-2d-array/#findComment-237972 Share on other sites More sharing options...
NewBob Posted April 25, 2007 Author Share Posted April 25, 2007 Perfect! Now it works just fine. Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/48597-solved-foreaching-a-2d-array/#findComment-237974 Share on other sites More sharing options...
taith Posted April 25, 2007 Share Posted April 25, 2007 no prob... its just that it registers both numbers and names... while() is programmed to work properly, foreach() isnt :-) Quote Link to comment https://forums.phpfreaks.com/topic/48597-solved-foreaching-a-2d-array/#findComment-237979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.