Removing last word from FOREACH loop

Posted by Stoosh on Stack Overflow See other posts from Stack Overflow or by Stoosh
Published on 2010-03-26T04:06:49Z Indexed on 2010/03/26 4:13 UTC
Read the original article Hit count: 464

Filed under:
|

I'm building a basic function, which builds out Mysql WHERE clauses based on how many are in the array.

$array = array('id' => '3', 'name' => 'roger'); 
$sql = "SELECT * FROM table WHERE ";

foreach ($array as $k => $v) {
    $sql .= $k . ' = ' . $v . ' AND ';
}

which will output

SELECT * FROM table WHERE id = 3 AND name = roger AND

However obviously I don't want that last AND, how do I go about removing it from the string?

Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about foreach