Grouping Array Results Out of Loop

2

How do I group Array results outside the loop in a single variable

  • In the example below the result that is inside the loop is correct but I need this same result to appear on the outside of the loop,

How can I make this work?

      foreach ($ids_cotados as $value) {
      echo $ids_cotados = $value.','; // 520, 130, 60, 700,

      }
      echo $ids_cotados;   // aqui da erro só pega 520,
    
asked by anonymous 14.08.2017 / 19:52

2 answers

1

When using something like $var = $novo within a loop what happens is at every loop / iteration the value of $var will be overwritten and after the loop will have the last value.

If you want to get the elements of the array and separate them with commas, you can use the implode() function in this case.

$arr = array(1,20,30,60,35);
echo implode(',', $arr);

Output:

1,20,30,60,35
    
14.08.2017 / 20:03
2

To do this, the best way is with implode() , it concatenates everything and places a string in the middle of each array value, in this case the ',' , would look like this:

$stringconcatenada = implode(",", $ids_cotados);
    
14.08.2017 / 20:02