Page 1 of 1

foreach with multidimensions - Confused

Posted: Wed Sep 14, 2005 9:37 am
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.

Posted: Wed Sep 14, 2005 9:54 am
by shiznatix
what happens when you echo that out?

Posted: Wed Sep 14, 2005 9:57 am
by jwalsh
Sorry, should have specified... It just returns nothing. Although, I know the $row['title'] is populated, I have already tested that.

Posted: Wed Sep 14, 2005 10:05 am
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 />";
	}
}

Posted: Wed Sep 14, 2005 10:05 am
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;
}

Posted: Wed Sep 14, 2005 10:07 am
by jwalsh
Thanks guys, I got it working :).