Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/48597-solved-foreaching-a-2d-array/
Share on other sites

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();

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.