dpp34603 2017-06-09 18:02
浏览 27
已采纳

too long

I have some code which pulls from multiple tables of a MS SQL database, arranges the data and outputs an array, I then need to use that array to populate a worksheet telling someone what items are needed on a day to day basis. Right now the output looks like the below, where the fields are ID, Name, Date, number (they are asterisk deliminated because some names include commas).

74*xxxx*2017-06-04*1
74*xxxx*2017-06-05*1
74*xxxx*2017-06-06*2
74*xxxx*2017-06-07*2
74*xxxx*2017-06-08*2
74*xxxx*2017-06-09*2
74*xxxx*2017-06-10*2
75*yyyy*2017-06-05*123
75*yyyy*2017-06-06*111
75*yyyy*2017-06-07*124
75*yyyy*2017-06-08*107
75*yyyy*2017-06-09*194
75*yyyy*2017-06-10*195

I am trying to get them to merge them into a single value per ID so that I can print it out to a grid to look similar to:

| ID | NAME | 06-04 | 06-05 | 06-06 | 06-07 | 06-08 | 06-09 | 06-10 |
| 74 | xxxx |   1   |   1   |   2   |   2   |   2   |   2   |   2   |
| 75 | yyyy |   -   |  123  |  111  |  124  |  107  |  194  |  195  | 

my intent was to merge the array values into a single array with the dates as the keys, the (most recent) code I was trying is:

$schedule[] = $PID . "*" . $program . "*" .  $date . "*" .  $num;


$grid = array();
foreach ($schedule as $item ) {

    $ex = explode('*', $item);
    $grid[$ex[0]] = array(
        'name' => $ex[1],
        $ex[2] => $ex[3],
    );
}

with the intent that that would merge them into a single array line to look like:

[74] => Array ( [name] => xxxx [2017-06-04] => 1 [2017-06-05] => 1 [2017-06-06] => 2 [2017-06-07] => 2 [2017-06-08] => 2 [2017-06-09] => 2 [2017-06-10] => 2)

after which I could just use it line by line with $grid[$i][1] etc to pull the value and print to each cell. I've tried other methods than the above, that is the only one I presently have the code snippet for and I've only been able to get it to keep the last value I add on a per ID basis.

I'm certain there is a piece I am missing or simply not understanding, if anyone could offer some input it would be appreciated.

  • 写回答

1条回答 默认 最新

  • duanlan3598 2017-06-09 18:21
    关注

    If I understood your requirement correctly, the below code will give the desired array.

    $schedule[] = '74*xxxx*2017-06-04*1';
    $schedule[] = '74*xxxx*2017-06-05*1';
    $schedule[] = '74*xxxx*2017-06-06*2';
    $schedule[] = '74*xxxx*2017-06-07*2';
    $schedule[] = '74*xxxx*2017-06-08*2';
    $schedule[] = '74*xxxx*2017-06-09*2';
    $schedule[] = '74*xxxx*2017-06-10*2';
    $schedule[] = '75*yyyy*2017-06-05*123';
    $schedule[] = '75*yyyy*2017-06-06*111';
    $schedule[] = '75*yyyy*2017-06-07*124';
    $schedule[] = '75*yyyy*2017-06-08*107';
    $schedule[] = '75*yyyy*2017-06-09*194';
    $schedule[] = '75*yyyy*2017-06-10*195';
    
    $grid = array();
    foreach ($schedule as $item ) {
        $ex = explode('*', $item);
        $grid[$ex[0]]['name'] = $ex[1];
        $grid[$ex[0]][$ex[2]] = $ex[3];
    }
    print_r($grid);
    

    And out put is

    Array
    (
        [74] => Array
            (
                [name] => xxxx
                [2017-06-04] => 1
                [2017-06-05] => 1
                [2017-06-06] => 2
                [2017-06-07] => 2
                [2017-06-08] => 2
                [2017-06-09] => 2
                [2017-06-10] => 2
            )
    
        [75] => Array
            (
                [name] => yyyy
                [2017-06-05] => 123
                [2017-06-06] => 111
                [2017-06-07] => 124
                [2017-06-08] => 107
                [2017-06-09] => 194
                [2017-06-10] => 195
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比