1

Hi can anyone help me through this,. I'm a beginner learning, please help me nest through foreach loop. Here is the code.

<?php 
                        $resource_url = "/app/resources/";
                        $names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
                    ?>

                        <ul>
                            <?php foreach ($names as $arr) {
                                $links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
                                foreach($links as $url){
                                echo "<li><a href=\"";
                                echo $resource_url; 
                                echo $url;
                                echo "\">";
                                echo $arr;
                                echo "</a></li>";
                                }
                            }?>

                        </ul> 
4
  • 1
    What is the problem ? This code doesn't work ? Please explain your problem with relevant details Commented Oct 21, 2015 at 8:18
  • Code works fine but values of first array $names are repeating Commented Oct 21, 2015 at 8:19
  • Having the loop within a loop will, I think, create a list 36 entries long. Commented Oct 21, 2015 at 8:22
  • Thats because your code runs through $links elements for every $names element. You need to use array_combine Commented Oct 21, 2015 at 8:23

4 Answers 4

1

Try this.

$base_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
    echo "<li><a href=\"";
    echo $base_url; 
    echo $key;
    echo "\">";
    echo $url;
    echo "</a></li>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

You've inserted the $links inside the foreach loop. Basically every time you loop one array item, ie. Affiliate Program, you loop the entire array of $links. Put $links outside the foreach loop or better yet.

<?php
$resource_url = "/app/resources/";
$names = array(
    'affiliate_program' => 'Affiliate program',
    'careers' => 'Careers',
    'corporate_info' => 'Corporate info',
    'eco' => 'Eco Initiative',
    'government' => 'Government Customers',
    'responsibility' => 'Social Responsibility');
?>

<ul>
    <?php foreach($names as $href => $arr) {

        echo "<li><a href=\"";
        echo $href;
        echo "\">";
        echo $arr;
        echo "</a></li>";
    }?>
</ul> 

1 Comment

Or - if for some reason - you have to start with the two separate arrays array_combine or MultipleIterator may be of use.
1

You can do this way -

$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
    echo "<li><a href=\"";
    echo $resource_url; 
    echo $key;
    echo "\">";
    echo $url;
    echo "</a></li>";
}

Or generate a single array (key => value) and loop through it.

1 Comment

Superb. Thank you.. Its Worked.!
0

If you want something like this:

click me to see the image

You can simply:

<?php 
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');

echo("<ul>");

for($i=0; $i < count($names); $i++){
    echo "<li><a href='";
    echo $resource_url; 
    echo $links[$i];
    echo "'>";
    echo $names[$i];
    echo "</a></li>";
}
echo("</ul>");
?>

1 Comment

@hemanthkumar by the way if one of these answers solved your problem you should mark it as solutions so that everyone knows which one solved the problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.