簡體   English   中英

如何使用3數組進行每個循環

[英]How to for-each loop using 3 array

 $sociallinkflied1 = $row['sociallinkflied'];
 $codes = explode(',', $sociallinkflied1);
 $sociallinktitle = $row['sociallink'];
 $names = explode(',', $sociallinktitle);
 $sociallinkflied = $row['sociallinkflied'];
 $sociallinkflied1 = explode(',', $sociallinkflied);

foreach( $codes as $index => $code ){
   echo '<a href='.$names[$index].' target=" '._blank.'" style="margin: 10px;">
 <i class="fa '.$code.'"></i>'.$sociallinkflied1[$index].'
 </a>';
                                               }

如何在每個循環中使用這3個數組。請幫助我解決此問題

我想您是在問如何在PHP中同時遍歷多個數組。 您認為需要通過索引引用數組項是正確的。

但是您需要使用for循環,因為索引是一個數字。 並且您必須通過索引引用所有數組:

$names = array("Tom", "Dick", "Harry");
$codes = array("aaa", "bbb", "ccc");
$links = array("/foo", "/bar", "/qux");

$number_of_items = count($names); # could use any of the arrays, if they are all the same size

for ($index = 0; $index < $number_of_items; $index++) {
   echo '<a href="'.$links[$index].'">
    <i class="fa '.$codes[$index].'"></i>'.$names[$index].'
</a>';
    echo "\n";
}

祝您程序順利!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM