我在php中有一个像这样的数组:
Array ( [0] => Array ( [post_date] => 2017-07-22 [num] => 1 )
[1] => Array ( [post_date] => 2017-07-24 [num] => 2 )
[2] => Array ( [post_date] => 2017-07-26 [num] => 5 ))
我想将其更改为此数组:
Array ( [0] => Array ( [post_date] => 2017-07-22 [num] => 1 )
[1] => Array ( [post_date] => 2017-07-23 [num] => 0 )
[2] => Array ( [post_date] => 2017-07-24 [num] => 2 )
[3] => Array ( [post_date] => 2017-07-25 [num] => 0 )
[4] => Array ( [post_date] => 2017-07-26 [num] => 5 ))
怎么做?
答案 0 :(得分:0)
希望我的帖子有用,我们正在使用PHP
Datetime
<?php
ini_set('display_errors', 1);
$array=
array (
0 =>
array (
'post_date' => '2017-07-22',
'num' => '1',
),
1 =>
array (
'post_date' => '2017-07-24',
'num' => '2',
),
2 =>
array (
'post_date' => '2017-07-26',
'num' => '5',
),
);
$dates= array_column($array, 'num','post_date');
$firstDate=$currentDate=key($dates);
end($dates);
$lastDate=key($dates);
$result=array();
while(true)
{
if(isset($dates[$currentDate]))
{
$result[]=array('post_date'=>$currentDate,'num'=>$dates[$currentDate]);
}
else
{
$result[]=array('post_date'=>$currentDate,'num'=>0);
}
if($currentDate==$lastDate)
{
break;
}
$dateTimeObject= new DateTime($currentDate);
$dateTimeObject->add(new DateInterval("P1D"));
$currentDate = $dateTimeObject->format("Y-m-d");
}
print_r($result);
答案 1 :(得分:0)
试试这段代码并希望它足够清楚,这个解决方案背后的想法非常简单:
post_date
列值的用户定义比较函数对数组进行排序,这将有助于我们以后循环日期。for
开始1
循环确保数组包含至少两个要比较的值,并确保数组的大小大于1
。如果不是,我们按原样返回数组。$ i = 1; $ i&lt;计数($阵列); =&GT; count($ array)&gt; 1 =&gt;至少2 值。
post_date
之间的差异,如果差异为1 day
,那么我们连续两天,否则我们必须在第二天将数据添加到数组中使用此帮助函数$ {“post_date
的位置:array_splice($ array,$ i,0,[[ 'post_date'=&gt; $ date1-&gt;修改('+ 1天') - &gt;格式('Y-m-d'), 'num'=&gt; 0, ]]);
这会将post_date
添加1天并将其他值推到数组右侧,这会使数组的大小更大(+1值),并为for循环添加另一个迭代。
在循环结束时,我们将获得OP想要的完整连续日数。
<?php
$array = [
['post_date' => '2017-07-22','num' => '1'],
['post_date' => '2017-07-26','num' => '5'],
['post_date' => '2017-07-24','num' => '2'],
];
sort($array);
$count = count($array);
for($i = 1; $i < $count; $i++) {
$date1 = new DateTime($array[$i-1]['post_date']);
$date2 = new DateTime($array[$i]['post_date']);
$diff = $date2->diff($date1)->format("%a");
if(1 < $diff) {
array_splice($array, $i, 0, [[
'post_date' => $date1->modify('+1 day')->format('Y-m-d'),
'num' => 0,
]]);
$count++;
}
}
echo "<pre>";
print_r($array);
输出:
Array
(
[0] => Array
(
[post_date] => 2017-07-22
[num] => 1
)
[1] => Array
(
[post_date] => 2017-07-23
[num] => 0
)
[2] => Array
(
[post_date] => 2017-07-24
[num] => 2
)
[3] => Array
(
[post_date] => 2017-07-25
[num] => 0
)
[4] => Array
(
[post_date] => 2017-07-26
[num] => 5
)
)
答案 2 :(得分:0)
试试这段代码。如果日期不合适,那么此代码也可以使用。我在代码中添加了注释以便理解
<?php
$array = array(
array(
"post_date"=>"2017-07-22",
"num" =>1
),
array(
"post_date"=>"2017-07-24",
"num" =>2
),
array(
"post_date"=>"2017-07-26",
"num" =>5
),
);
$total = count($array);
$check_dates = array_column($array, "post_date"); //array of dates
asort($check_dates); //sort array by dates and keep keys
$start_date = reset($check_dates); //minimum date
$end_date = end($check_dates); //maximum date
$current_date = $start_date; //set current date to start date
$new_array = array(); //this will be your final array
while (strtotime($current_date) <= strtotime($end_date)) {
$search_val = array_search($current_date, $check_dates);
if($search_val > 0 || $search_val === 0)
{
$new_array[] = $array[$search_val];
}
else
{
$tmp_array=["post_date"=>$current_date,"num"=>0];
$new_array[] = $tmp_array;
}
$current_date = date("Y-m-d",strtotime('+1 day',strtotime($current_date))); //change current date
}
print_r($new_array);
答案 3 :(得分:0)
您可以定义一个增加大小的数组,该数组能够包含整个数据集。然后,您可以推送其中的所有元素,一旦使用if([already selected]) {
viewHolder.likebtn.setBackground([your selected drawable]);
} else {
viewHolder.likebtn.setBackground([your non selected drawable]);
}
,您可以按照自己的意愿对数组进行排序。
strtotime
你重复这么多次你的数组包含元素,以确保你有正确的继承。希望这有帮助!