drpfu51608120170 2014-12-10 06:14
浏览 8
已采纳

从PHP数组中获取数据

I have an array that has the following structure. There are two elements with the exact same date/time the difference is the direction and the value.

Array
(
    [0] => Array
        (
            [date] => 2014-12-09
            [time] => 21:58:15
            [value] => 4
            [in_out] => Outgoing
        )
    [1] => Array
        (
            [date] => 2014-12-09
            [time] => 21:58:15
            [value] => 5
            [in_out] => Incoming
        )
    [2] => Array
        (
            [date] => 2014-12-09
            [time] => 21:57:45
            [value] => 5
            [in_out] => Outgoing
        )
    [3] => Array
        (
            [date] => 2014-12-09
            [time] => 21:57:45
            [setup_name] => 5
            [in_out] => Incoming
        )
    [4] => Array
        (
            [date] => 2014-12-09
            [time] => 21:57:15
            [setup_name] => 3
            [in_out] => Incoming
        )
    [5] => Array
        (
            [date] => 2014-12-09
            [time] => 21:57:15
            [value] => 6
            [in_out] => Outgoing
        )
    [6] => Array
        (
            [date] => 2014-12-09
            [time] => 21:56:45
            [value] => 6
            [in_out] => Outgoing
        )
    [7] => Array
        (
            [date] => 2014-12-09
            [time] => 21:56:45
            [value] => 7
            [in_out] => Incoming
        )
    [8] => Array
        (
            [date] => 2014-12-09
            [time] => 21:56:15
            [value] => 4
            [in_out] => Outgoing
        )
    [9] => Array
        (
            [date] => 2014-12-09
            [time] => 21:56:15
            [value] => 5
            [in_out] => Incoming
        )
)

I need get the data into a new array with the following structure:

Array
(
    [0] => Array
        (
            [date] => 2014-12-09
            [time] => 21:58:15
            [out_value] => 4
            [in_value] => 5
        )
    [1] => Array
        (
            [date] => 2014-12-09
            [time] => 21:57:45
            [out_value] => 5
            [in_value] => 5
        )
    [2] => Array
        (
            [date] => 2014-12-09
            [time] => 21:57:15
            [out_value] => 6
            [in_value] => 3
        )
    [3] => Array
        (
            [date] => 2014-12-09
            [time] => 21:56:45
            [out_value] => 6
            [in_value] => 7
        )
    [4] => Array
        (
            [date] => 2014-12-09
            [time] => 21:56:15
            [out_value] => 4
            [in_value] => 5
        )
)

It would be simple enough if the outgoing/incoming alternated consistently but sometimes there will be two incoming's together (3 and 4).

Any ideas would be appreciated.

Thanks

  • 写回答

2条回答 默认 最新

  • duanhuiqing9528 2014-12-10 06:42
    关注

    First please let me make sure that at within one second, there is only one Incoming and one Outgoing?

    If your answer is yes, please try this:

    $input = array
    (
        '0' => array
            (
                'date' => '2014-12-09',
                'time' => '21:58:15',
                'value' => '4',
                'in_out' => 'Outgoing'
            ),
        '1' => array
            (
                'date' => '2014-12-09',
                'time' => '21:58:15',
                'value' => '5',
                'in_out' => 'Incoming'
            ),
        '2' => array
            (
                'date' => '2014-12-09',
                'time' => '21:57:45',
                'value' => '5',
                'in_out' => 'Outgoing',
            ),
        '3' => array
            (
                'date' => '2014-12-09',
                'time' => '21:57:45',
                'setup_name' => '5',
                'in_out' => 'Incoming'
            ),
        '4' => array
            (
                'date' => '2014-12-09',
                'time' => '21:57:15',
                'setup_name' => '3',
                'in_out' => 'Incoming'
            ),
        '5' => array
            (
                'date' => '2014-12-09',
                'time' => '21:57:15',
                'value' => '6',
                'in_out' => 'Outgoing'
            ),
        '6' => array
            (
                'date' => '2014-12-09',
                'time' => '21:56:45',
                'value' => '6',
                'in_out' => 'Outgoing'
            ),
        '7' => array
            (
                'date' => '2014-12-09',
                'time' => '21:56:45',
                'value' => '7',
                'in_out' => 'Incoming'
            ),
        '8' => array
            (
                'date' => '2014-12-09',
                'time' => '21:56:15',
                'value' => '4',
                'in_out' => 'Outgoing'
            ),
        '9' => array
            (
                'date' => '2014-12-09',
                'time' => '21:56:15',
                'value' => '5',
                'in_out' => 'Incoming'
            )
    );
    $temp = array();
    $keys = array('Incoming' => 'in_value', 'Outgoing' => 'out_value');
    foreach ($input as $line){
        $temp[$line['date'].$line['time']]['date'] = $line['date'];
        $temp[$line['date'].$line['time']]['time'] = $line['time'];
    
        if(!empty($line['value'])){
            $temp[$line['date'].$line['time']][$keys[$line['in_out']]] = $line['value'];
        }elseif(!empty($line['setup_name'])){
            $temp[$line['date'].$line['time']][$keys[$line['in_out']]] = $line['setup_name'];
        }
    }
    $result = array_values($temp);
    print_r($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?