How to use multiple arrays in " single " foreach() loop

51,805

Solution 1

assume you have the same number of items in those two arrays.

if you want to use foreach(), then the arrays need to have the same index:

foreach ($a as $index => $value)
{
    echo $a[$index] .' '. $b[$index];
}

if the arrays have numeric index, you can just use for():

for ($i = 0; $i < sizeof($a); $i++)
{
    echo $a[$i] .' '. $b[$i];
}

Solution 2

array_combine() will merge your 2 arrays into one with key=>value pairs and then access them through foreach statement e.g

foreach(array_combine($a,$b) as $key=>$value) {
    echo $key."<br/>".$value;
}

Solution 3

The answer depends on what exactly you need to do with the arrays and how you need to do it.

If your arrays have similar indexes, and you need to use the same element from each array, you could do like

foreach ($array1 as $index => $value1) {
    $value2 = $array2[$index];
    // do stuff with $value1 and $value2 here
}

(Although in this case, particularly if you find yourself doing this a lot, you may want to think about using a single array of either objects or arrays, so that the data will always be together and easier to connect.)

Or, if the arrays have the same type of elements in it and you want to iterate over both in order, you could iterate over array_merge($array1, $array2). (If the arrays aren't numerically indexed, though, and particularly if they have the same stringy keys, one of the arrays' elements could replace the other. See the docs on array_merge for details.)

There are a bunch of other possibilities, depending on how you need the elements. (The question really doesn't provide any info on that.)

Solution 4

Full Code:

1)

<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');

foreach($First as $indx => $value) {
    echo $First[$indx].$Second[$indx];
    echo "<br />";
}
?>

or 2)

<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');

for ($indx = 0 ; $indx < count($First); $indx ++) {
  echo $First[$indx] . $Second[$indx];
  echo "<br />";
}
?>
Share:
51,805
yusufiqbalpk
Author by

yusufiqbalpk

I am a student of Islamic studies. Studying web based applications in leisure having beginner experience in HTML, CSS, JSP, jQuery, PHP, SQL and MySQL

Updated on January 21, 2020

Comments

  • yusufiqbalpk
    yusufiqbalpk almost 4 years

    I'm trying to echo out all variables of two arrays with a single foreach loop making a url. Some coding examples will be honored.

    I have this so far :

    <?php
    foreach($menu_names as $menu_name){
    echo "<li><a href='?subj= " .urlencode($subjects["id"]). " '>".$menu_name."</a></li>";
    }
    ?>
    

    I want to add one more array within this loop

    • yusufiqbalpk
      yusufiqbalpk over 11 years
      @JohnConde Check out. I have edited my Question
    • yusufiqbalpk
      yusufiqbalpk over 11 years
      @Candy some examples please !
    • FluorescentGreen5
      FluorescentGreen5 over 6 years
      @candyleung foreach loops are faster
  • cHao
    cHao over 11 years
    Note, if you use for, the arrays need to have the same indexes too. You don't really gain much, other than semantically separating the index and avoiding saying $a[$index] when you already have it (it's $value).
  • yusufiqbalpk
    yusufiqbalpk over 11 years
    Thanks I was in need of that.