在php中嵌套foreach循环

时间:2015-10-21 08:15:59

标签: php loops foreach

嗨,任何人都可以帮助我解决这个问题。我是初学者,请帮我嵌套foreach循环。这是代码。

<?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 个答案:

答案 0 :(得分:1)

您已在foreach循环中插入$ links。基本上每次循环一个数组项,即。联盟计划,你循环整个$ link链接。将$ links放在foreach循环之外或更好。

<?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 :(得分:1)

你可以这样做 -

$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>";
}

或生成单个数组(key =&gt; value)并循环遍历它。

答案 2 :(得分:1)

试试这个。

$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>";
}

答案 3 :(得分:0)

如果你想要这样的东西:

click me to see the image

你可以简单地说:

<?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>");
?>