dsdfd2322
2013-05-02 08:33 阅读 40
已采纳

如果两个日期匹配,则在特定日期合并两个数组

I am merging the two arrays at particular date. If both array have same date then it will merge the array & plot that array on the graph. But problem is that it is merging the array & appending the bar at the start of the bar only, but not at the specific date where the both date are equal. for e.g.

array1 = (18/03/2013 => 10, 20-03-2013 => 6, 21-03-2013 => 10);
array2 = (20-03-2013 => 5);

. so it should append the bar at 20-03-2013 but actually it is appending the bar at the start only i.e. 18-03-2013.

plz help me thanks in advance

hear is my code

   // gives the how many calls has came per day
$count = __Select("tbl_call_master","COUNT(DATE(date_time)) AS call_count ,  DATE(date_time)AS date ","WHERE DATE(date_time) BETWEEN      '$from_date' AND '$to_date' GROUP BY DATE(date_time) ");

$get_first_array=array(); //created the array to store the result


while($row = mysql_fetch_array($count))
{
    // daily records are been saved in record1[] array  
    $record1[]= array(
        $row['date'],
        $row['call_count']
    );

}
// gives the how many calls has came per day where status is WIP(work in progress) 
$wip=  __Select("tbl_call_master","COUNT(DATE(date_time)) AS  call_count , DATE(date_time) AS date ","WHERE status= 'WIP' AND DATE(date_time) BETWEEN '$from_date' AND '$to_date' GROUP BY DATE(date_time) ");

while($row= mysql_fetch_array($wip))
{
    //daily records are been saved in wiprecord[] array 
    $wiprecord[]= array(
        $row['date'],
        $row['call_count']
    );

}
$chk=0;
// foreach runs till the records are there  
foreach ( $record1 as  $key=> $value ) { 

// it will $chk is 1 if
     if($chk==1){ 
         $get_first_array[$key] =  $record1[$key]; //this record1 is gets transfer in another array which  i am going to show in graph
     }
//foreach runs till the wiprecords are there    
foreach ( $wiprecord as  $key=> $value1) {


end($wiprecord);// it will give the last index of wiprecord

    $last=key($wiprecord); // last index is stored in last variable
    if($key==$last ){ // if key is last then it will make the $chk to 1
        $chk=1;
    }
    if($value[0] == $value1[0] ) // checks whether date of record1 & date of wiprecord equals then it will enter in the condition
    {
        array_push($record1[$key], $wiprecord[$key][1]); // this will put the contents in record1[]
        $get_first_array[$key] = $record1[$key]; // this record1 is gets transfer in another array
        print_r( $get_first_array[$key]);   
    }
 }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongzhuo2010 dongzhuo2010 2013-05-02 14:51

    I've made changes to your code:

    1. Removed $chk and the if condition.
    2. Added new variable $date_is_matching.
    3. Reduced the complexity of the code.

    Replace $chk=0; till foreach($record1 as $key=> $value){}//Opening to closing with following:

    $date_is_matching=0; 
    //If date in $record1 is present in $wiprecord set $date_is_matching as 1.
    foreach ( $record1 as  $key=>$value ) {
        foreach ($wiprecord as  $key1=> $value1) {
            if($value[0] == $value1[0]) {
                $date_is_matching=1;
                array_push($record1[$key], $wiprecord[$key1][1]);
            }
        }
        if($date_is_matching!=1) {
            array_push($record1[$key], 0);
        }
        $get_first_array[$key] =  $record1[$key];
        $date_is_matching=0;
    } //But now $get_first_array = $record1;
    

    This is how I've visualized your problem and solution: link


    Update:
    $fill_date = array(); //New variable for adding missed dates and values(0).
    for($i=0;$i<count($get_first_array)-1;$i++){
        $j = $i+1;
        $diff[$i] = GetDays($get_first_array[$i][0], $get_first_array[$i+1][0]); //Gets the dates between the two.
        if(!empty($diff[$i]) && is_array($diff[$i])) {
            foreach($diff[$i] as $date) {
                $fill_date[] = array(0=>$date,1=>0,2=>0); //Push the date and values
            }
        }
    }
    $get_first_array = array_merge($get_first_array,$fill_date); //Merge with the final array.
    var_dump($get_first_array);
    

    Function GetDays():

    function GetDays($sStartDate, $sEndDate) {
      // Firstly, format the provided dates. This function works best with YYYY-MM-DD
      // but other date formats will work thanks to strtotime().
      $sStartDate = strtotime($sStartDate);
      $sEndDate = strtotime($sEndDate);
    
      // If $sStartDate is bigger than $sEndDate, Then swap $sStartDate and $sEndDate
      if($sStartDate>$sEndDate) {
          $ttime = $sStartDate;
          $sStartDate = $sEndDate;
          $sEndDate = $ttime;
      }
    
      $sStartDate = gmdate("Y-m-d", $sStartDate);
      $sEndDate = gmdate("Y-m-d", $sEndDate);
    
      // Start the variable off with the start date
      //$aDays[] = $sStartDate;
    
      // Set a 'temp' variable, sCurrentDate, with the start date - before beginning the loop
      $sCurrentDate = $sStartDate;
    
      // While the current date is less than the end date
      while($sCurrentDate < $sEndDate){
        // Add a day to the current date
        $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));
        // Add this new day to the aDays array
        if($sCurrentDate != $sEndDate)
            $aDays[] = $sCurrentDate;
      }
    
      // Once the loop has finished, return the array of days.
      return $aDays;
    }
    


    Input arrays:
    $record1 = array(0 => array(0=>'2013-03-18',1=>10), 
                     1 => array(0=>'2013-03-20',1=>6), 
                     2 => array(0=>'2013-03-21',1=>10), 
                     3=>array(0=>'2013-03-24',1=>10));
    
    $wiprecord = array(0 => array(0=>'2013-03-20',1=>5), 
                       1 => array(0=>'2013-03-21',1=>5));
    


    Output array:

    $get_first_array = array ( 0 => array ( 0 => 2013-03-18 1 => 10 2 => 0 )
                               1 => array ( 0 => 2013-03-20 1 => 6 2 => 5 )
                               2 => array ( 0 => 2013-03-21 1 => 10 2 => 5 )
                               3 => array ( 0 => 2013-03-24 1 => 10 2 => 0 )
                               4 => array ( 0 => 2013-03-19 1 => 0 2 => 0 )
                               5 => array ( 0 => 2013-03-22 1 => 0 2 => 0 )
                               6 => array ( 0 => 2013-03-23 1 => 0 2 => 0 ) ) ;
    
    点赞 评论 复制链接分享

相关推荐