0

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;

?>

Edited by mike_2000_17: Fixed formatting

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by mindboggler2010
0

IMHO, this is simpler:

$arr_pcs = array("3408","1203","3421");
echo "'" . implode("','",$arr_pcs) . "'";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.