我在php中有这样的数组
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
)
)
我必须制作这个数组
如果我选择了两个月from_month
,to_month
如果此数组中没有任何月份,则应该包含它,例如:
我选择2014年5月至2014年2月5日。但如果在我的阵列中只有2014年2月,2014年4月,2014年可能只会如此iwat将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
}
答案 0 :(得分:1)
循环遍历它并测试是否缺少值:
$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
}
编辑:TS首先要求对数组进行排序,使用自定义排序的代码可以在本文的编辑中找到。
答案 1 :(得分:1)
最好的方法是从您输入的开始日期和结束日期开始参考,并在主数组中查找..
这是(请滚动回答)
$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);
OUTPUTS
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
)
)