[eluser]xwero[/eluser]
why create a string if you need an array?
Code:
function call_cat_name(){
$arr_cat_id=array();
$arr_category=array();
$query=$this->book_model->get_cat_name();
foreach($query as $row){
$arr_cat_id[] = $row['cat_id'];
$arr_category[] = $row['cat_name'];
}
return array_combine($arr_cat_id,$arr_category);
}
If you create a string with a postfix in a loop you know you need to cut off the postfix, an easy way to do it is to use the substr function
Code:
$category = "";
$comma=",";//separate by commas // make the variable as useful as possible
foreach($query as $row){
$category .= $row['cat_name'];
$category .= $comma;
}
$category = substr($category,0,-1);
from the split function php documentation
Quote:preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.
That snippet you showed has quite a number of beginners mistakes and removing the last array element is the least.