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
https://forums.phpfreaks.com/topic/241285-arrays-loops/
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
https://forums.phpfreaks.com/topic/241285-arrays-loops/#findComment-1239387
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.