Jump to content

Arrays / Loops


Andy11548

Recommended Posts

Hello,

 

I'm trying to make 2 arrays with a connection to each other and I want them to loop together so that the arrays are connected.

 

$page_title = array('Home', 'About Us', 'Members');
$page_link = array('index', 'about', 'members');

 

I want them to automatically convert it so it's like:

 

<a href="index.php">Home</a> || <a href="about.php">About Us</a> || <a href="members.php">Members</a>

 

How could I make it so that it automatically makes it like that, from arrays?

 

If there is a way, or another way which is simular, help would be greatly appriciated.

 

Thanks,

Andy.

Link to comment
Share on other sites

You have 2 options: a key'd array where you pick one or the other to be the key, or just an array of arrays.  Since I don't see you accessing this array by keys, I'm just going to show you the array of arrays.

 

Something like this should work:

$menu = array(array('title' => 'Home', 'link' => 'index'), array('title' => 'About Us', 'link' => 'about'), array('title' => 'Members', 'link' => 'members'));

// make your links

$menuhtml = '';
foreach ($menu as $menuitem) {
  $menuhtml .= '' . $menuitem['title'] . ' ||';
}
echo rtrim($menuhtml, '|');

Link to comment
Share on other sites

Thank you very, very much. This is exactly what I was looking for.

 

I'll have to revise and write the method to make sure that I remember it.

 

You have a been a great help and I thank you alot.

 

Thanks again,

Andy.

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.