doufangyan6862 2013-11-20 05:36
浏览 36
已采纳

比较数组中的先前值和当前值

I am trying to create a new CSV file from an existing CSV file with additional data. I used fgetcsv to access the data from the existing CSV file and put it inside an array. After putting the csv file contents to an array, the array turned out as a 2-D array with data that looks like this:

Array {
     [0] =>
        Array {
            [0] => "Data1", "Data2", Data3
        }
}

So what I did was create a new array from this by imploding first the array values then exploding like so:

for($i = 0; $i < $myArray; $i++) {
    $tempString = implode(" ", $myArray);
    $tempArray = explode (" ", $tempString);
}

With this my array now looks like so:

Array {
      [0] => "Data1"
      [1] => "Data2"
      [3] => "Data3" //endDate
}

Array {
      [0] => "Data1"
      [1] => "Data2"
      [3] => "Data3" //endDate
}

Now, I would like to add a fourth value on the array for startDate. I want the startDate to be equal to the endDate + 1. So for example the endDate is 20th November, then the endDate for the next Array is 21st November. The thing is I'm not entirely sure how to implement that. I've tried adding this inside the for loop just like pretty much all the examples and articles I've read:

$d3 = $tempArray[3]; //Data3
if($d3 == $previous) {
      //do some things here
}
$previous = $d3 + 1;

The thing with this is the dates tend to be in groups. For example arrays 1-8 is 20th November then array 9-20 is 30th November. Because of this, only the first date among these "groups" get the previous value so only array 9 would get 21st November. How can I tell my code that while the end date is the same, it does not change the start date? Then if it does, it should get the previous date and add 1 day to it? Please help.

Also, I know there might be something wrong with how I'm handling the arrays so if anybody knows how to handle this better, faster and more efficiently, I would appreciate it.

  • 写回答

1条回答 默认 最新

  • douzhi2012 2013-11-20 06:23
    关注
    $resultant_array = array(
                            array(
                                '0' => 'hello',
                                '1' => 'world',
                                '2' => '20-11-2009'
                            ),
                            array(
                                '0' => 'hi',
                                '1' => 'check',
                                '2' => '20-11-2009'
                            ),
                            array(
                                '0' => 'hi',
                                '1' => 'newcheck',
                                '2' => '30-11-2009'
                            ),
                            array(
                                '0' => 'hell0',
                                '1' => 'newcheck',
                                '2' => '30-11-2009'
                            )
                        );
    foreach($resultant_array as $rk => &$rv) {
        if($rk == 0) {
            $end_date = $rv[2];
        }
        if($rv[2] == $end_date) {
            continue; // ignore as its neigther the first group 
        } else {
            if($rk) $rv[] = date('d-m-Y', strtotime('+1 day',strtotime($rv[2])));
            $end_date = $rv[2];
        }
    }
    

    This is just a sample iteration and you should also think of parsing the csv contents in a much better way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器