Hi,

I just want to share this function to you that can split an array and return it and formatted as: 'element1','element2' etc.

This is useful in your WHERE clause particulary if you are using IN
e.g. select id, lastname, firstname from farmers_table where id in ([B]'3408', '1223'[/B]);

The BOLD letters above is the return format of this function..

Enjoy. Hope it helps :)

function convert_INstr($test_str) {
$isthere_acomma = stripos($test_str,",");

if ($isthere_acomma > 0) {
    if (strlen($test_str) > 0) {
        $arr_teststr = split(",",$test_str);
        if (count($arr_teststr) > 0) {
            foreach($arr_teststr as $mx) {
                $in_str .= "'" . $mx . "',";
            }
        }
        $in_str = substr($in_str,0,(strlen($in_str)-1));

    }   
} else {
    $in_str = $test_str;
}//end of if ($isthere_acomma > 0) statement

return $in_str;

}

This is how to use the function

<?php

$arr_pcs = array("3408","1203","3421");
$imp_pbihid = implode(",",$arr_pcs);
$str_format_split = convert_INstr($imp_pbihid);
echo $str_format_split;

?>

Recommended Answers

All 3 Replies

IMHO, this is simpler:

$arr_pcs = array("3408","1203","3421");
echo "'" . implode("','",$arr_pcs) . "'";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.