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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.