My foreach only finds the last element of the array

arrays, foreach, nav, php

Solution

That is becuase of same index elements in your array........

<?php
echo '<nav id="main_nav">';
$links = array(
    '0' => 'Home',
    '1' => 'About Us',
    '2' => 'Our Services',
    '3' => 'Portfolio',
    '4' => 'Testimonials',
    '5' => 'Gallery',
    '6' => 'Contact Us'
);
foreach($links as $href => $label){
    echo '<a href="',$href,'">',$label,'</a>';
}
echo '</nav>';

?>

and the answer is `<nav id="main_nav"><a href="0">Home</a><a href="1">About Us</a><a href="2">Our Services</a><a href="3">Portfolio</a><a href="4">Testimonials</a><a href="5">Gallery</a><a href="6">Contact Us</a></nav> `

Problem

This is my first time here. I have a problem with my foreach loop, it only outputs the "Contact Us" link and none of the others. I can't see a problem with my syntax: ``` <?php echo '<nav id="main_nav">'; $links = array( '#' => 'Home', '#' => 'About Us', '#' => 'Our Services', '#' => 'Portfolio', '#' => 'Testimonials', '#' => 'Gallery', '#' => 'Contact Us' ); foreach($links as $href => $label){ echo '<a href="',$href,'">',$label,'</a>'; } echo '</nav>'; ?> ```

Original source