数组合并去重的一个问题

时间:2022-09-03 17:26:32
现在的数组如下

Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                        )
                )
        )
                
        [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                        )
                )
        )
        

    [2] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )
                        )
                )
        )
        [3] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )
                        )
                )
        )
        [4] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )
                )
        )
)



想合并成下面这样的结果


Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                                                        [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.1
                                )
                        )
                    [1] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )
                        )
                )
        )
                
        [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                              [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.2
                                )
                        )
                    [1] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )
                        )
                )
        )
        [2] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )
                )
        )
)



小弟不才,求指点

8 个解决方案

#1


该回复于2016-12-27 09:03:40被管理员删除

#2


$ar = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                 
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
         
 
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);
foreach($ar as $v){
if(!isset($ret[$v['ip']])){
$ret[$v['ip']] = $v;
}else{
foreach($ret[$v['ip']]['ports'] as $kl=>$vl) $ret[$v['ip']]['ports'][$kl]['service'] = array('name' => 'unknown', 'title' =>$v['ip']  );
foreach($v['ports'] as $port) $ret[$v['ip']]['ports'][] = $port;
}
}
print_r(array_values($ret));

#3



<?php
$arr = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                  
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
          
  
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);

$result = array();

foreach($arr as $k=>$v){
    if(isset($result[$v['ip']])){
        $result[$v['ip']]['ports'][] = $v['ports'][0];
        $result[$v['ip']]['ports'][0]['service'] = array(
            'name' => $v['ports'][0]['service']['name'],
            'title' => $v['ip']
        );
    }else{
        $result[$v['ip']] = $v;
    }
}

$result = array_values($result);
print_r($result);


输出


Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.1
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.2
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [2] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )

                )

        )

)

#4


构造一个hashmap,把要去重的字段作为hashmap的key,都放进hashmap自然去重复

#5



<?php 
$b=array
(
    array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => array
                (
                    array
                        (
                            'port' => '1122',
                            'status' => 'open',
                            'ttl' => '63'
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => array
                (
                   array
                        (
                            'port' => '3304',
                            'service' => array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd'
                                )
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => array
                (
                   array
                        (
                            'port' => '3344',
                            'status' => 'open',
                            'ttl' => '63'
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => array
                (
                   array
                        (
                            'port' => '3306',
                            'service' => array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd'
                                )
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => array
                (
                array
                        (
                            'port' => '7788',
                            'status' => 'open',
                            'ttl' => '63'
                        )
                )
        )
);
$result = array();
foreach($b as $v){
    $isflag=false;
    foreach($result as $k => $v1){
        if(isset($v1['ip'])&&$v['ip']==$v1['ip']){
            $isflag=true;
            break;
        }
    }
    if($isflag){
            $result[$k]['ports'][]=$v['ports'][0];
            $result[$k]['ports'][0]['service']=array(
                'name' => $v['ports'][0]['service']['name'],
                'title' => $v['ip']
            );
        } else {
            $result[]=$v;
    }
}
echo "<pre>";
print_r($result);
echo "<pre/>";
?>

#6



<?php
$arr = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                  
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
          
  
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);

$result = array();

foreach($arr as $k=>$v){
    if(isset($result[$v['ip']])){
        $result[$v['ip']]['ports'][] = $v['ports'][0];
        $result[$v['ip']]['ports'][0]['service'] = array(
            'name' => $v['ports'][0]['service']['name'],
            'title' => $v['ip']
        );
    }else{
        $result[$v['ip']] = $v;
    }
}

$result = array_values($result);
print_r($result);


输出


Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.1
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.2
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [2] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )

                )

        )

)

#8


参考代码:

<?php
$arr = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                  
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
          
  
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);

$result = array();

foreach($arr as $k=>$v){
    if(isset($result[$v['ip']])){
        $result[$v['ip']]['ports'][] = $v['ports'][0];
        $result[$v['ip']]['ports'][0]['service'] = array(
            'name' => $v['ports'][0]['service']['name'],
            'title' => $v['ip']
        );
    }else{
        $result[$v['ip']] = $v;
    }
}

$result = array_values($result);
print_r($result);


输出


Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.1
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.2
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [2] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )

                )

        )

)

#1


该回复于2016-12-27 09:03:40被管理员删除

#2


$ar = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                 
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
         
 
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);
foreach($ar as $v){
if(!isset($ret[$v['ip']])){
$ret[$v['ip']] = $v;
}else{
foreach($ret[$v['ip']]['ports'] as $kl=>$vl) $ret[$v['ip']]['ports'][$kl]['service'] = array('name' => 'unknown', 'title' =>$v['ip']  );
foreach($v['ports'] as $port) $ret[$v['ip']]['ports'][] = $port;
}
}
print_r(array_values($ret));

#3



<?php
$arr = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                  
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
          
  
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);

$result = array();

foreach($arr as $k=>$v){
    if(isset($result[$v['ip']])){
        $result[$v['ip']]['ports'][] = $v['ports'][0];
        $result[$v['ip']]['ports'][0]['service'] = array(
            'name' => $v['ports'][0]['service']['name'],
            'title' => $v['ip']
        );
    }else{
        $result[$v['ip']] = $v;
    }
}

$result = array_values($result);
print_r($result);


输出


Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.1
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.2
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [2] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )

                )

        )

)

#4


构造一个hashmap,把要去重的字段作为hashmap的key,都放进hashmap自然去重复

#5



<?php 
$b=array
(
    array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => array
                (
                    array
                        (
                            'port' => '1122',
                            'status' => 'open',
                            'ttl' => '63'
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => array
                (
                   array
                        (
                            'port' => '3304',
                            'service' => array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd'
                                )
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => array
                (
                   array
                        (
                            'port' => '3344',
                            'status' => 'open',
                            'ttl' => '63'
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => array
                (
                   array
                        (
                            'port' => '3306',
                            'service' => array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd'
                                )
                        )
                )
        ),
    array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => array
                (
                array
                        (
                            'port' => '7788',
                            'status' => 'open',
                            'ttl' => '63'
                        )
                )
        )
);
$result = array();
foreach($b as $v){
    $isflag=false;
    foreach($result as $k => $v1){
        if(isset($v1['ip'])&&$v['ip']==$v1['ip']){
            $isflag=true;
            break;
        }
    }
    if($isflag){
            $result[$k]['ports'][]=$v['ports'][0];
            $result[$k]['ports'][0]['service']=array(
                'name' => $v['ports'][0]['service']['name'],
                'title' => $v['ip']
            );
        } else {
            $result[]=$v;
    }
}
echo "<pre>";
print_r($result);
echo "<pre/>";
?>

#6



<?php
$arr = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                  
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
          
  
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);

$result = array();

foreach($arr as $k=>$v){
    if(isset($result[$v['ip']])){
        $result[$v['ip']]['ports'][] = $v['ports'][0];
        $result[$v['ip']]['ports'][0]['service'] = array(
            'name' => $v['ports'][0]['service']['name'],
            'title' => $v['ip']
        );
    }else{
        $result[$v['ip']] = $v;
    }
}

$result = array_values($result);
print_r($result);


输出


Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.1
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.2
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [2] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )

                )

        )

)

#7


#8


参考代码:

<?php
$arr = Array
(
    '0' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 1122,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        ),
                  
        '1' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3344,
                            'status' => 'open',
                            'ttl' => 63
                        )
                )
        ),
          
  
    '2' => Array
        (
            'ip' => '192.168.1.1',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3304,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '3' => Array
        (
            'ip' => '192.168.1.2',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 3306,
                            'service' => Array
                                (
                                    'name' => 'unknown',
                                    'title' => 'awdawdawd',
                                )
                        )
                )
        ),
        '4' => Array
        (
            'ip' => '192.168.1.3',
            'timestamp' => '1482719333',
            'ports' => Array
                (
                    '0' => Array
                        (
                            'port' => 7788,
                            'status' => 'open',
                            'ttl' => 63,
                        )
                )
        )
);

$result = array();

foreach($arr as $k=>$v){
    if(isset($result[$v['ip']])){
        $result[$v['ip']]['ports'][] = $v['ports'][0];
        $result[$v['ip']]['ports'][0]['service'] = array(
            'name' => $v['ports'][0]['service']['name'],
            'title' => $v['ip']
        );
    }else{
        $result[$v['ip']] = $v;
    }
}

$result = array_values($result);
print_r($result);


输出


Array
(
    [0] => Array
        (
            [ip] => 192.168.1.1
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 1122
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.1
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3304
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [1] => Array
        (
            [ip] => 192.168.1.2
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 3344
                            [status] => open
                            [ttl] => 63
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => 192.168.1.2
                                )

                        )

                    [1] => Array
                        (
                            [port] => 3306
                            [service] => Array
                                (
                                    [name] => unknown
                                    [title] => awdawdawd
                                )

                        )

                )

        )

    [2] => Array
        (
            [ip] => 192.168.1.3
            [timestamp] => 1482719333
            [ports] => Array
                (
                    [0] => Array
                        (
                            [port] => 7788
                            [status] => open
                            [ttl] => 63
                        )

                )

        )

)