简体   繁体   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>';
                                               }

How To used this 3 array in for each loop.please help me solve this issue 如何在每个循环中使用这3个数组。请帮助我解决此问题

I think you're asking how to loop over multiple arrays at the same time in PHP. 我想您是在问如何在PHP中同时遍历多个数组。 You are correct in thinking you need to refer to the array items by index. 您认为需要通过索引引用数组项是正确的。

But you need to use a for loop because the index is a number. 但是您需要使用for循环,因为索引是一个数字。 And you must refer to all of the arrays by index: 并且您必须通过索引引用所有数组:

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

Good luck with your program! 祝您程序顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM