2017-08-06 48 views
0

0值我在PHP这样的数组:获得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)) 

如何做到这一点?

+1

你试过了什么? –

+0

我相信我对你的问题有一个更加完善的解决方案,但是当你的问题处于搁置状态时,我无法发布。请改善您的问题与所需的细节,以便它可以重新打开。 (如果它重新开放,请ping我) – mickmackusa

回答

0

试试这个代码,并希望这是很清楚,这个解决方案背后的想法很简单:

  • 首先我们用post_date列值对用户定义的比较函数进行排序,这将有助于我们在以后的循环在日期。
  • 之后,我们通过从1开始for循环并确保数组的大小大于1,确保数组至少包含两个要比较的值。如果不是,我们会按照原样返回数组。

$ i = 1; $ i < count($ array); => count($ array)> 1 =>至少有2个 的值。

  • 接下来我们计算阵列的两个连续post_date之间的差异,如果差异1 day所以我们有连续两天,否则我们要在第二天从添加到阵列“$ I”通过使用该助手功能的post_date位置:

array_splice($阵列,$ I,0,[[ 'POST_DATE'=> $ date1->修改('+ 1一天) - >格式('Ym-d'), 'num'=> 0, ]]);

这将增加1天post_date和推其它值到所述阵列的,这使得该阵列的尺寸更大的(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 
     ) 

) 
0

希望我的帖子会有所帮助,这里我们使用PHPDatetime

Try this code snippet here

<?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); 
0

试试这个code.If日期是不是为了再也是这个代码工作。我已在代码中添加注释以便了解

<?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); 
0

您可以定义一个数组,其大小可以包含整个数据集。然后您可以将所有元素都推入其中,一旦这样做,使用strtotime,您可以按照您的意愿对数组进行排序。

<?php 
    $arr1 = $your_initial_array; 
    $arr2 = array_push($arr1, $your_values); 
    $arr3 = array(); 
    $topop = 0; 
    for($x = 0; $x < count($arr2); $x++) { 
     for($i = 0; $i < count($arr2); $i++) { 
      if(strtotime($arr2[$i]['post_date'] > $latest) { // or < if you want 
       $topop = $i; 
      } 
     } 
     array_push($arr3, $arr2[$topop]); 
     unset($arr2[$topop]); 
    } 

你重复它很多次你的数组包含元素,以确保你有正确的继承。希望这可以帮助!