如果php数组中没有月,则包括月
我在php中有这样一个数组如果php数组中没有月,则包括月,php,date,Php,Date,我在php中有这样一个数组 Array ( [0] => Array ( [month] => April-2014 [total_booking] => 2 ) [1] => Array ( [month] => May-2014 [total_booking] => 5 )
Array
(
[0] => Array
(
[month] => April-2014
[total_booking] => 2
)
[1] => Array
(
[month] => May-2014
[total_booking] => 5
)
[2] => Array
(
[month] => June-2014
[total_booking] => 25
)
[3] => Array
(
[month] => October-2013
[total_booking] => 1
)
[4] => Array
(
[month] => July-2014
[total_booking] => 4
)
)
我必须制作这个数组
如果我选择了从_month到_month的两个月,如果此数组中没有任何月份,则应将其包括在内,例如:
我选择了2014年2月至2014年5月。但如果在我的数组中只有2014年2月、2014年4月、2014年5月,那么我只能将2014年3月包含在正确的位置。
像这样
Array
(
[month] => March-2014
[total_booking] => 0
)
这是我的密码
foreach ($newarray as $month => total_booking) {
//sorting
}
foreach ($newarray as $month => total_booking) {
//if there is no month in array betwean to_month and from_month it
should be included in correct place as sorted
}
循环检查并测试是否缺少值:
$prev_month = false;
foreach($your_array as $k=>$values){
if($prev_month!==false){ // first itteration will be empty, dont do useless checks
// Test if themonth after the previous month matches this one
if( $prev_month+1 !== $values['month'] ){
// FILL IN THE BLANKS
// splice in at position $thisIndex (<- this you'll have to do)
array_splice( $prev_month, $thisIndex-1, 0, $newDateBookingsArray); // -1, we want it before this item!
// (dont forget to loop in case more than 1 items miss)
}
}
$prev_month = $values['month']; // save value for next round
}
$prev_month=false;
foreach($k=>$value的_数组){
如果($prev_month!==false){//first iteration将为空,请不要执行无用的检查
//测试上个月后的月份是否与此月份匹配
如果($prev_month+1!=$values['month'])){
//填空
//在$thisIndex位置拼接(最好的方法是从输入的开始日期和结束日期中引用,并在主数组中查找。
在这里(请滚动查看答案)
你在问如何排序?@putvande no.,,,,…实际上,这不是我的要求,我想包括排序日期之前的所有月份。例如:我选择了从2014年2月到2014年5月的。但是如果在我的数组中只包含2014年2月、2014年4月、2014年5月的rch
在正确的位置,您要求“1.将此数组排序”我的答案的第二部分回答了你问题的第二部分,这意味着我想按排序加入。我的错误..我的qn令人困惑..编辑了我的qn我已经编辑了我的帖子。包括了一个在你想要的点添加值的方法,这样就不需要重新旋转。我建议你在第一个代码块中删除你的数组,并使用像$your_array,我差点错过了你实际上有代码(我以为你是手工做的;))对不起,先生..我没有理解你..我英语很差:)我自己做的。请用小写。
$your_array=array
(
array
(
'month' => 'April-2014',
'total_booking' => 2
),
array
(
'month' => 'May-2014',
'total_booking' => 5
),
array
(
'month' => 'June-2014',
'total_booking' => 25
),
array
(
'month' => 'October-2013',
'total_booking' => 1
),
array
(
'month' => 'July-2014',
'total_booking' => 4
)
);
$start_date="Jan 1 2013";
$end_date="Dec 31 2014";
$timestamp1=strtotime($start_date);
$timestamp2=strtotime($end_date);
for($i=$timestamp1;$i<$timestamp2;$i=$i+24*60*60){
//echo $i;
$gapmonth[]=date('F-Y',$i);
}
$gapmonth=array_unique($gapmonth);
//convert $dataS(ORIGINAL ARRAY) to one dimentional array so the life will be easier
foreach($your_array as $val){
$derived_val[$val['month']]=$val['total_booking'];
}
foreach($gapmonth as $val){
if(array_key_exists($val,$derived_val)){
$total_booking=$derived_val[$val];
}
else{
$total_booking=0;
}
$finaldate[]=array('month'=>$val,'total_booking'=>$total_booking);
}
echo "<pre>";
print_r($finaldate);
Array
(
[0] => Array
(
[month] => January-2013
[total_booking] => 0
)
[1] => Array
(
[month] => February-2013
[total_booking] => 0
)
[2] => Array
(
[month] => March-2013
[total_booking] => 0
)
[3] => Array
(
[month] => April-2013
[total_booking] => 0
)
[4] => Array
(
[month] => May-2013
[total_booking] => 0
)
[5] => Array
(
[month] => June-2013
[total_booking] => 0
)
[6] => Array
(
[month] => July-2013
[total_booking] => 0
)
[7] => Array
(
[month] => August-2013
[total_booking] => 0
)
[8] => Array
(
[month] => September-2013
[total_booking] => 0
)
[9] => Array
(
[month] => October-2013
[total_booking] => 1
)
[10] => Array
(
[month] => November-2013
[total_booking] => 0
)
[11] => Array
(
[month] => December-2013
[total_booking] => 0
)
[12] => Array
(
[month] => January-2014
[total_booking] => 0
)
[13] => Array
(
[month] => February-2014
[total_booking] => 0
)
[14] => Array
(
[month] => March-2014
[total_booking] => 0
)
[15] => Array
(
[month] => April-2014
[total_booking] => 2
)
[16] => Array
(
[month] => May-2014
[total_booking] => 5
)
[17] => Array
(
[month] => June-2014
[total_booking] => 25
)
[18] => Array
(
[month] => July-2014
[total_booking] => 4
)
[19] => Array
(
[month] => August-2014
[total_booking] => 0
)
[20] => Array
(
[month] => September-2014
[total_booking] => 0
)
[21] => Array
(
[month] => October-2014
[total_booking] => 0
)
[22] => Array
(
[month] => November-2014
[total_booking] => 0
)
[23] => Array
(
[month] => December-2014
[total_booking] => 0
)
)