How can I use the contents of a variable that originated in a foreach, outside of it in PHP?

0

My case is as follows: Inside a foreach I download the contents of a mysql query and assign it to a variable $ preferences (it is a series of numbers, in this case 7 and 8 with an echo print 78)

then I want to use that variable to put it as part of a where condition of a query (where user_id in (7, 8)). I need to extract the contents of the variable $ preferences How can I get it out and separate them by commas? Up to now I have tried the following, but I still can not get the foreach variable to use it in the query:

foreach($filaCRUD as $fila){
        $preferencia=", ".$fila->por_usuario_id;
        echo "<td><strong>".$preferencia."</strong></td>";
}

in advance, thanks for your help.

    
asked by H Iván Sánchez Lobillo 30.11.2017 в 05:23
source

1 answer

1

try this

foreach($filaCRUD as $fila){
    $preferencia=", ".$fila->por_usuario_id;
    echo "<td><strong>".$preferencia."</strong></td>";
    $tmp[] = $fila->por_usuario_id;
}
echo "prefencia: ".implode(",", $tmp);

Greetings!

    
answered by 30.11.2017 / 06:13
source