0

I'm trying to read an array to save some values but it doesnt work! Here's my code:

$array=$_POST['idprod'];//I get my array and save it on a var 
print_r($array); //It has ALL the data (I use a print_r($array); And YES!! It has the information i need)
$ids[]=explode(',',$array);//Substring to my var 

for( $contador=0; $contador <count($ids); $contador++ ) 
{ 
echo $ids[$contador].'<br/>'; 
} 

It shows me Array to string conversion in...

What could I do?

Jean
  • 593
  • 2
  • 6
  • 19

2 Answers2

2

use foreach Instead for loop The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement


foreach ($array => $var ) {
    // do any thing
}

replace

$ids[]=explode(',',$array);//Substring to my var 
for( $contador=0; $contador <count($ids); $contador++ ) { 
echo $ids[$contador].'<br/>'; 

and set

$ids = explode(',',$array);
foreach($ids as $id) {
    echo $id ."<br>";
}
ANZAWI
  • 79
  • 1
  • 5
0

You only need to explode(), if the incomming data is a comma separated string.

$string = $_POST['idprod'];

$array= explode(',', $string); // split the comma separated values into an array

for($i=0; $i<count($array); $i++) 
{ 
    echo $array[$i] . '<br/>'; 
} 

Else you can directly work with the incomming array:

$array = $_POST['idprod']; 

for($i=0; $i<count($array); $i++) 
{ 
   echo $array[$i] . '<br/>'; 
} 
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141