Jump to content


Photo

Nested Arrays Question - little stuck!


  • Please log in to reply
2 replies to this topic

#1 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 12:44 PM

Hi

Consider the following code, this is simply for testing purposes until i get this right.

As you can see, i have an array manually setup for testing here called $pops_info which contains ID, NAME, and NO_SITES.
Further down I setup an array with field information called $arr, this is going to be used to table generation. The options are: fields (number of fields with headings) direction (verticle table or horizontal) field_headings (this contains another array with the table field headings) field_contents (this i want to contain the contents from the $pops_info array). The next few lines are obviously loops that display the relevant array info, but Im stuck on creating a loop that will display each of the $pops_info by accessing $arr['field_contents']  am i making sense?

<?
$pops_info['id'][0] = 1;
$pops_info['name'][0] = "Name Test";
$pops_info['no_sites'][0] = 2;	

$pops_info['id'][1] = 2;
$pops_info['name'][1] = "Name Test 2";
$pops_info['no_sites'][1] = 5;

// Display
$arr = array (
	'fields' => 2,
	'direction' => 'vert',
	'field_headings' => array ("Company Name","Number Of Sites"),
	'field_contents' => array($pops_info)
);

foreach ($arr as $a) {
	echo $a."<br>";
}

foreach ($arr['field_headings'] as $fh) {
	echo $fh . "<br>";
}

?>

http://thewebsolutionprovider.com - A Solution For Everything Web!

#2 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 01:01 PM

i figured that the following will display whats in the field_contents array, but how do i put this through a loop?

echo $arr['field_contents'][0]['name'][0];
echo $arr['field_contents'][0]['name'][1];

http://thewebsolutionprovider.com - A Solution For Everything Web!

#3 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 October 2006 - 01:03 PM

ahah, we have joy!

for ($i=0;$i<count($arr['field_contents'][0]);$i++) {
	echo $arr['field_contents'][0]['name'][$i];
}

http://thewebsolutionprovider.com - A Solution For Everything Web!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users