Petr Fořt Fru-Fru
Petr Fořt Fru-Fru

Reputation: 996

How to concatenate an array with a 'foreach' loop?

I have several arrays. example:

$a = Array(                                     
      [0] => #ID_1
      [1] => #ID_2
      [2] => #ID_3
      )
$b = Array(
     [0] => ABC
     [1] => cde
     [2] => fgh
     )
$c = Array(
     [0] => 000
     [1] => 111
     [2] => 222
     )

How to concatenate these data fields using any foreach, for, while loop?

Expected result:

$result = '#ID_1 ABC 000';

I try something like this:

$result = '';

foreach($a as $aa) {
   $result .= $aa ." ";

     foreach ($b as $bb) {
        $result .= $bb ." ";

           foreach($c as $cc) {
              $result .= $cc .'<br>';
           }
       }
  }

but the result did not meet expectations. Can anyone advise how to use foreach to chain these arrays together? Thank you. Thank you very much.

Upvotes: 1

Views: 1536

Answers (5)

GeniusGeek
GeniusGeek

Reputation: 335

using for loop this way would give you your expected result

<?php 
$a = Array(                                     
 0 => '#ID_1',
  1 => '#ID_2',
  2 => '#ID_3'
  );
$b = Array(
 0 => 'ABC',
 1 => 'cde',
 2 => 'fgh'
 );
$c = Array(
 0 => '000',
 1 => '111',
 2 => '222'
 );
$arrlengtha = count($a);
$arrla = $arrlengtha - 2;

$arrlengthb = count($b);
$arrlb = $arrlengthb - 2;

$arrlengthc = count($c);
$arrlc = $arrlengthc - 2;

for($x = 0; $x < $arrla; $x++) {
echo $a[$x]." ";
}

 for($x = 0; $x < $arrlb; $x++) {
echo $b[$x]." ";
}

 for($x = 0; $x < $arrlc; $x++) {
  echo $c[$x]." ";
 }

?>

Upvotes: 1

Prifulnath
Prifulnath

Reputation: 567

Way to do this using foreach, if number of elements in each array is same.

$arr1 = ["#ID_1", "#ID_2", "#ID_3"];
$arr2 = ["ABC", "DEF", "FGH"];
$arr3 = ["000", "111", "222"];

foreach($arr1 as $k => $item) {
    echo "$item {$arr2[$k]} {$arr3[$k]}<br>";
}

Upvotes: 1

Aksen P
Aksen P

Reputation: 4599

If you need foreach loop then use next code:

$res = [];

 foreach($a as $ind=>$val){
     $res[$ind] = $val." ".$b[$ind]." ".$c[$ind]; 
 } 

Demo

Upvotes: 4

BCM
BCM

Reputation: 675

You can do something like this.

$arr1 = ["#ID_1", "#ID_2", "#ID_3"];
$arr2 = ["ABC", "DEF", "FGH"];
$arr3 = ["000", "111", "222"];

$arrayLength = count($arr1);

$i = 0;
$result = [];
while ($i < $arrayLength)
{
    $result[] = $arr1[$i]." ".$arr2[$i]." ".$arr3[$i];
    $i++;

}

foreach($result as $item){
    echo $item."<br/>";
}

Upvotes: 2

Juan V
Juan V

Reputation: 508

According to what you're asking, you want to join elements with the key 0 from all arrays, then with the key 1 and so on, so instead of doing a foreach loop you could use a for loop using the key:

<?php 

    $a = Array(                                     
        0 => "#ID_1",
        1 => "#ID_2",
        2 => "#ID_3",
    );
    $b = Array(
        0 => "ABC",
        1 => "cde",
        2 => "fgh"
    );
    $c = Array(
        0 => 000,
        1 => 111,
        2 => 222
    );

    $length = sizeof($a);

    $output = '';

    for($i = 0; $i<$length; $i++) {
        $output .= $a[$i] . ' / ' . $b[$i] . ' / ' . $c[$i] . '<br>';
    }

    echo $output;

?>

Outputs:

#ID_1 / ABC / 0
#ID_2 / cde / 111
#ID_3 / fgh / 222

Upvotes: 1

Related Questions