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 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi