TheEmperor TheEmperor - 2 months ago 15
MySQL Question

How to use where in statement in codeigniter with pair of values from multidimensional array

I have the following array:

$chain_combination = array (array('a','b'), array ('c','d'), array('e','f'));


How I can make the following query in codeigniter by using the $chain_combination array?

SELECT *
FROM table
WHERE (column1, column2) IN (('a', 'b'), ('c', 'd'), ('e','f'))

Answer
$arr = array(array('a','b'), array ('c','d'), array('e','f'));

echo "<pre>";
print_r($arr);


$out = array();
foreach( $arr as $key=>$val ) {
    if( is_array($val) ) {
        $out[] = "('".implode("','",$val)."')";
    }   
}    

$final = implode(",",$out);

print_r($final);


//out: ('a','b'),('c','d'),('e','f') 

with foreach and implode

Comments