简体   繁体   中英

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

I think you're asking how to loop over multiple arrays at the same time in 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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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