Jump to content

[SOLVED] Array help


The Little Guy

Recommended Posts

My code is doing a few things wrong:

1. It is misspelling "Visitor Information" as "Bisitor Information"

2. It is not making my sub array(s).

 

Current Code:

$links['create'] = 'New Project';
$links['settings'] = 'Settings';
$links['stats?page=visitor'] = 'Visitor Information';
$links['stats?page=visitor']['&sub=browser'] = 'Browser Information';
$links['stats?page=traffic'] = 'Traffic Information';

echo '<div style="line-height:20px;">';
foreach($links as $link => $text){
echo '<div><a href="'.$link.'">'.$text.'</a></div>';
if(is_array($link)){
	foreach($link as $l => $t){
		echo $link.$l.'<br>';
	}
}
}
echo '</div>';

 

Output:

New Project
Settings
Bisitor Information
Traffic Information

Link to comment
https://forums.phpfreaks.com/topic/124437-solved-array-help/
Share on other sites

You can't make a sub-array out of a string.

 

See here:

$links['stats?page=visitor'] = 'Visitor Information';

 

You are making the element stats?page=visitor a string, by assigning it the value of 'Visitor Information'.

 

Then here:

$links['stats?page=visitor']['&sub=browser'] = 'Browser Information';

 

You are trying to assign an array onto the string, which does not work.

 

I think you may need to re-think the idea. This should work:

 

$links['create'] = 'New Project';
$links['settings'] = 'Settings';
$links['stats?page=visitor'] = array('Visitor Information');
$links['stats?page=visitor']['&sub=browser'] = 'Browser Information';
$links['stats?page=traffic'] = 'Traffic Information';

echo '<div style="line-height:20px;">';
foreach($links as $link => $text){
	if(is_array($text)){
		foreach($text as $l => $t){
			echo '<div><a href="'.$link.($l == "0" ? "" : $l).'">'.$t.'</a></div>';
		}
	}else{
		echo '<div><a href="'.$link.'">'.$text.'</a></div>';
	}

}

echo '</div>';

 

I did a bit of re-ordering and changed a few things, not sure if that's how you want it though.

Link to comment
https://forums.phpfreaks.com/topic/124437-solved-array-help/#findComment-642611
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.