1

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.

5 Answers 5

4

If you need foreach loop then use next code:

$res = [];

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

Demo

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

1

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

Comments

1

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]." ";
 }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.