我有以下代码。 我有一个月的数组。
$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'].",";
}
假设$ m = 1,2,3,4,5,
然后,我爆炸并创建数组。
$paid_month = explode(",",$m);
$arr=array();
foreach($paid_month as $key=>$p)
{
array_push($arr,$p);
}
然后,我打印不在$arr
中的月份名称。
foreach($months as $key=>$a)
{
if(!in_array($key,$arr))
{
echo $a.",";
//echo $months[$key];
}
}
但我认为,这种方法是为了它的长期。那么这样做的替代方法是什么?
答案 0 :(得分:0)
在代码中:
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'].",";
}
你可以改变:
$m .= $count_month['month'].",";
到
$m[] = $count_month['month'];
这将避免需要再次爆炸()
答案 1 :(得分:0)
试试这个:
$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 . ',';
}
您不需要将$ m连接为字符串然后将其爆炸
还要注意我的$months
数组中的一个小变化。我删除了钥匙。但是要从1
值开始保留它们,我只需在第一个(零)位置添加一个空值。
答案 2 :(得分:0)
这样的东西?
$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);