Jump to content

Archived

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

glenelkins

Nested Arrays Question - little stuck!

Recommended Posts

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?

[code]
<?
$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>";
}

?>
[/code]

Share this post


Link to post
Share on other sites
i figured that the following will display whats in the field_contents array, but how do i put this through a loop?

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

Share this post


Link to post
Share on other sites
ahah, we have joy!

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

Share this post


Link to post
Share on other sites

×

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.