foreach with multidimensions - Confused

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

foreach with multidimensions - Confused

Post by jwalsh »

Hi,

I'm working on parsing some mysql rows into a multidimensional array, but I'm having a bit of confusion... I think I am close though.

Code: Select all

$a = 0;
		$result = DB_query("SELECT * FROM inquiry_data WHERE parent=0 ORDER BY title");
		while ($row = DB_fetchArray($result)) {
			$InquiryData[$a]['id'] = $row['id'];
			$InquiryData[$a]['business'] = $row['business'];
			$InquiryData[$a]['uid'] = $row['uid'];
			$InquiryData[$a]['rand_id'] = $row['rand_id'];
			$InquiryData[$a]['title'] = $row['title'];
			$InquiryData[$a]['content'] = $row['content'];
			$InquiryData[$a]['status'] = $row['status'];
			
			$a++;
		}
Now, I would think i could access the elements using foreach, like this...

Code: Select all

foreach ($InquiryData as $CurrentInquiry) {
echo $CurrentInquiry['title'];
}
What am I overlooking? Thanks.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

what happens when you echo that out?
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Sorry, should have specified... It just returns nothing. Although, I know the $row['title'] is populated, I have already tested that.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

try this first

Code: Select all

echo "<pre>";
print_r($inquiryData);
echo "</pre>";
foreach ($InquiryData as $CurrentInquiry) { 
	if (is_array($currentInquiry)){
		echo "<pre>";		
		print_r($currentInquiry);
		echo "</pre>";
	}else{
		echo "currentInquiry is not an array<br />";
	}
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

do a print_r on each array and see if they are populated as you expect them to be

also, when you do the foreach, you're trying to use it like its still an associative array, which it is not, it will be a numerically indexed array.

so

Code: Select all

foreach($blah AS $blah2)
{
   echo $blah2['title'];
}

// IS WRONG!

// do this

foreach($blah AS $blah2)
{
   echo $blah2;
}
Last edited by s.dot on Wed Sep 14, 2005 10:07 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Thanks guys, I got it working :).
Post Reply