繁体   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