2015-10-21 173 views
1

嗨,任何人都可以幫助我通過這個。我是初學者,請幫助我通過foreach循環嵌套。這是代碼。在PHP中嵌套的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> 
+1

什麼問題?此代碼不起作用?請解釋您的問題並提供相關詳細信息 – Tushar

+0

代碼正常工作,但第一個數組$名稱的值正在重複 –

+0

在循環中創建一個36個條目的列表。 – RamRaider

回答

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

您已經在foreach循環中插入了$鏈接。基本上每次你循環一個數組項,即。聯盟計劃,您循環整個$鏈接陣列。將$鏈接放在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> 
+0

或者 - 如果由於某種原因 - 您必須從兩個單獨的數組[array_combine](http://docs.php.net/array_combine)或[MultipleIterator](httpd://docs.php .net/manual/en/class.multipleiterator.php)可能會有用。 – VolkerK

+0

哦,非常感謝你....它的工作,.. –

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

或生成一個數組(鍵=>值)和環路。

+0

棒極了。謝謝..它的工作。 –

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

@hemanthkumar很高興它的工作! :) –

+0

@hemanthkumar順便說一句,如果其中一個答案解決了您的問題,您應該將其標記爲解決方案,以便每個人都知道哪一個解決了問題:) –