Jump to content

[SOLVED] foreaching a 2d array


NewBob

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.