Cristofaro
Cristofaro

Reputation: 3

Variable in array and value separed with comma

My code is:

$arr = array(1,2,3);
foreach ($arr as $key => $value) 
{
  $resultstr[] = "FIND_IN_SET (" .$value. ", Dotazioni) > 0";   
}
$dotazioni_camera = implode(" and ",$resultstr);
echo($dotazioni_camera);

Result is:

FIND_IN_SET (1, Dotazioni) > 0 and FIND_IN_SET (2, Dotazioni) > 0 and FIND_IN_SET (3, Dotazioni) > 0

This is correct!! But if i try to change Arr from

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

To

 $arr = array($var);

the result change in:

FIND_IN_SET (1,2,3, Dotazioni) > 0

I think that variable convert integer value separeted by comma in a string value. I looked for a possible solution but I did not find anything. I hope you can help me.

Upvotes: 0

Views: 46

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

If you have

$var = '1,2,3';

When you call

$arr = array($var);

This is an array of 1 value - 1,2,3. If you want to have an array of values 1, 2 and 3, you will have to do something like...

$var = '1,2,3';
$arr = explode(",", $var);

Upvotes: 2

Related Questions