i have a following code. i have a array of month.
$m="";
$months = array(1=>'January',2=>'February',3=>'March',4=>'April',5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',10=>'October',11=>'November',12=>'December');
foreach($objhst_fees->fetch_hst_fees("`month`,`fees`","where `grno`='".$stu_name['grno']."'")as $count_month)
{
//value like:$count_month['month']=1,2,3
$m .= $count_month['month'].",";
}
so assume $m=1,2,3,4,5,
then,i explode it and create array.
$paid_month = explode(",",$m);
$arr=array();
foreach($paid_month as $key=>$p)
{
array_push($arr,$p);
}
and then, i print the month name which is not in $arr
.
foreach($months as $key=>$a)
{
if(!in_array($key,$arr))
{
echo $a.",";
//echo $months[$key];
}
}
but i think,this method is to long for it.so what is the alternatives for doing this?
In the code:
foreach($objhst_fees->fetch_hst_fees("`month`,`fees`","where `grno`='".$stu_name['grno']."'")as $count_month) {
//value like:$count_month['month']=1,2,3
$m .= $count_month['month'].",";
}
you could change:
$m .= $count_month['month'].",";
to
$m[] = $count_month['month'];
which will avoid need to explode() it again
Try this:
$m = array();
$months = array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
foreach( $objhst_fees->fetch_hst_fees("`month`,`fees`","where `grno`='".$stu_name['grno']."'")as $count_month)
{
$m[] = $count_month['month'];
}
foreach ($months as $key => $month) {
if (in_array($key, $m)) {
continue;
}
echo $month . ',';
}
You don't need to concatenate the $m as string and then explode it.
Also notice a small change in my $months
array. I removed the keys. But to preserve them starting with 1
value I simply added an empty value at first (zero) position.
Something like this?
$paid_month = explode(",", $m);
$paid_months = array();
foreach($paid_month as $p)
{
$paid_months[$p] = (isset($months[$p]) ? $months[$p] : 'Unknown');
}
print_r($paid_months);