Read Array out of foreach

2

In this script below I read an array inside the foreach, but how can I read it off? when I give an echo it only returns me 1 id

 $data = unserialize ($linha["range_ids"]); 
 // a:3:{i:1052;s:4:"1052";i:1053;s:4:"1053";i:1054;s:4:"1054";}

 foreach($data as $key => $value): //Ler a Variavel 
  echo      $range = $key.',';  // Retorno do echo 1052,1053,1054, 
 endforeach; 

 echo $range; // Retorno do echo 1054   (Preciso que retorne assim 1052,1053,1054)
    
asked by anonymous 19.10.2015 / 17:49

3 answers

1

Simple, use an easier way to foreach the array.

$lista = array('1052','1053','1054');
$a = "";
foreach($lista as $b){
 $a .= $b . ',';
}
echo $a; // Resultado: 1052,1053,1054

See the PHP: Array

    
19.10.2015 / 18:01
2

To do this, use the print_r function.

example:

$meu_array = array(1, 2, 3);

print_r($_POST);

print_r($meu_array); // Array (3) { 1, 2, 3 }

You can also use the implode

echo implode(',', array(1, 2, 3)); // Imprime 1, 2, 3
    
19.10.2015 / 17:53
1

The variable $range is having the value overlapped each time the foreach runs.

What you seem to need is to add to the existing value, something like this:

$range = "";

foreach($data as $key => $value):
  echo      $range = $range + $key.',';  
 endforeach; 

 echo $range;
    
19.10.2015 / 17:53