根据另一个数组设置数组值

时间:2015-01-20 16:46:01

标签: php arrays foreach

我有两个数组:$bluescan$maclistResults$bluescan遗失了一些['company']个值,我需要通过搜索$maclistResults来更新。

$bluescan有30个子阵列,$maclistResults是每个制造商的列表,这些制造商曾向IEEE注册了一个MAC地址块,总共20285个子阵列。

$bluescan数组:

Array
(
    [0] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 640
            [mac] => 00:16:98:B0:12:64
            [company] => T&A Mobile Phones
            [date_seconds] => 1420077635639
            [title] => ALCATEL ONE TOUCH POP C5
            [timestamp] => 1420077691701
            [latitude] => -15.8524528
            [longitude] => -48.9588197
            [altitude] => 764.9000244140625
            [provider] => fused
            [rssi] => -95
        )

    [1] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 642
            [mac] => B0:35:8D:F6:95:61
            [company] => Nokia Corporation
            [date_seconds] => 1420077664750
            [title] => 202
            [timestamp] => 1420077783525
            [latitude] => -15.852453
            [longitude] => -48.9588198
            [altitude] => 764.9000244140625
            [provider] => fused
            [rssi] => -92
        )

    [2] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 639
            [mac] => B4:52:7E:A2:39:24
            [company] => 
            [date_seconds] => 1420077631422
            [title] => C2304 BT
            [timestamp] => 1420077875407
            [latitude] => -15.8524667
            [longitude] => -48.9588193
            [altitude] => 762.7999877929688
            [provider] => fused
            [rssi] => -99
        )

    [3] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 641
            [mac] => D4:8F:33:B7:02:A4
            [company] => 
            [date_seconds] => 1420077664483
            [title] => Nokia225
            [timestamp] => 1420077877692
            [latitude] => -15.8524666
            [longitude] => -48.9588192
            [altitude] => 762.7999877929688
            [provider] => fused
            [rssi] => -81
        )

    [4] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 637
            [mac] => 76:05:EC:B6:0D:18
            [company] => 
            [date_seconds] => 1420077584596
            [timestamp] => 1420077947070
            [latitude] => -15.8526392
            [longitude] => -48.9586783
            [altitude] => 766.4000244140625
            [provider] => fused
            [rssi] => -64
        )

    [5] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 638
            [mac] => 04:18:0F:F1:62:82
            [company] => Samsung Electronics Co.,Ltd
            [date_seconds] => 1420077625693
            [timestamp] => 1420078626690
            [latitude] => -15.8526245
            [longitude] => -48.9586099
            [altitude] => 767.7000122070313
            [provider] => fused
            [rssi] => -97
        )

    [6] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 645
            [mac] => 00:1B:FB:84:A6:01
            [company] => Alps Electric Co., Ltd
            [date_seconds] => 1420080593817
            [timestamp] => 1420080593817
            [latitude] => -15.852645
            [longitude] => -48.9585741
            [altitude] => 766.0
            [provider] => fused
            [rssi] => -90
        )

    [7] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 647
            [mac] => 68:48:98:9B:C9:E5
            [company] => 
            [date_seconds] => 1420081331918
            [title] => Reinaldo Luz (Galaxy S
            [timestamp] => 1420081334914
            [latitude] => -15.8526664
            [longitude] => -48.9584509
            [altitude] => 769.9000244140625
            [provider] => fused
            [rssi] => -93
        )

    [8] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 646
            [mac] => 18:46:17:34:3D:2A
            [company] => Samsung Electronics
            [date_seconds] => 1420080986423
            [title] => C3222
            [timestamp] => 1420081500612
            [latitude] => -15.8526021
            [longitude] => -48.9586167
            [altitude] => 758.5999755859375
            [provider] => fused
            [rssi] => -89
        )

    [9] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 648
            [mac] => 0C:14:20:C2:EF:0E
            [company] => Samsung Electronics Co.,Ltd
            [date_seconds] => 1420081774680
            [title] => Samsung S5222
            [timestamp] => 1420081774680
            [latitude] => -15.8525864
            [longitude] => -48.9585792
            [altitude] => 749.5999755859375
            [provider] => fused
            [rssi] => -76
        )

    [10] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 649
            [mac] => 00:12:1C:B0:17:11
            [company] => PARROT S.A.
            [date_seconds] => 1420081957160
            [timestamp] => 1420081957160
            [latitude] => -15.8525838
            [longitude] => -48.9585793
            [altitude] => 749.5
            [provider] => fused
            [rssi] => -82
        )

    [11] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 651
            [mac] => 60:E1:F8:2E:99:58
            [company] => 
            [date_seconds] => 1420082126387
            [timestamp] => 1420082132244
            [latitude] => -15.8526099
            [longitude] => -48.958569
            [altitude] => 755.2999877929688
            [provider] => fused
            [rssi] => -93
        )

    [12] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 653
            [mac] => E0:75:0A:14:82:B9
            [company] => 
            [date_seconds] => 1420082598249
            [timestamp] => 1420082632504
            [latitude] => -15.8525498
            [longitude] => -48.9586453
            [altitude] => 763.9000244140625
            [provider] => fused
            [rssi] => -100
        )

    [13] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 652
            [mac] => 50:FC:9F:31:FC:95
            [company] => 
            [date_seconds] => 1420082559412
            [timestamp] => 1420083040325
            [latitude] => -15.8525465
            [longitude] => -48.9586551
            [altitude] => 763.7000122070313
            [provider] => fused
            [rssi] => -74
        )

    [14] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 654
            [mac] => 34:C7:31:E4:5B:35
            [company] => ALPS Co,. Ltd.
            [date_seconds] => 1420083215566
            [timestamp] => 1420083215566
            [latitude] => -15.852421
            [longitude] => -48.9586235
            [altitude] => 757.2999877929688
            [provider] => fused
            [rssi] => -97
        )

    [15] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 655
            [mac] => 74:5E:1C:6C:26:F7
            [company] => PIONEER CORPORATION
            [date_seconds] => 1420083215735
            [title] => AVH-265BT
            [timestamp] => 1420083256624
            [latitude] => -15.8521723
            [longitude] => -48.958702
            [altitude] => 760.7000122070313
            [provider] => fused
            [rssi] => -75
        )

    [16] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 657
            [mac] => 40:BA:61:C3:06:6D
            [company] => Arima Communications Corp.
            [date_seconds] => 1420083274599
            [title] => X350MONTEIRO
            [timestamp] => 1420083274599
            [latitude] => -15.8521477
            [longitude] => -48.9587804
            [altitude] => 759.2000122070313
            [provider] => fused
            [rssi] => -75
        )

    [17] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 659
            [mac] => 00:0D:92:0D:C6:41
            [company] => Arima Communication Corporation
            [date_seconds] => 1420083444880
            [timestamp] => 1420083444880
            [latitude] => -15.851529
            [longitude] => -48.9585946
            [altitude] => 741.2000122070313
            [provider] => fused
            [rssi] => -89
        )

    [18] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 660
            [mac] => C0:65:99:CA:15:FE
            [company] => Samsung Electronics Co.,Ltd
            [date_seconds] => 1420083561134
            [title] => C3222
            [timestamp] => 1420083561134
            [latitude] => -15.8509498
            [longitude] => -48.9583767
            [altitude] => 0.0
            [provider] => fused
            [rssi] => -98
        )

    [19] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 661
            [mac] => 10:F9:6F:27:B9:4E
            [company] => LG Electronics
            [date_seconds] => 1420083571135
            [title] => Talis
            [timestamp] => 1420083612114
            [latitude] => -15.8510159
            [longitude] => -48.9583372
            [altitude] => 745.0
            [provider] => fused
            [rssi] => -91
        )

    [20] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 663
            [mac] => 6C:2F:2C:14:BE:13
            [company] => 
            [date_seconds] => 1420083751261
            [title] => Roquia Maria (GT-S5312
            [timestamp] => 1420083751261
            [latitude] => -15.8505985
            [longitude] => -48.9583914
            [altitude] => 751.0
            [provider] => fused
            [rssi] => -96
        )

    [21] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 664
            [mac] => 30:76:6F:92:FA:6B
            [company] => LG Electronics
            [date_seconds] => 1420083790225
            [title] => LG-E455f
            [timestamp] => 1420083790225
            [latitude] => -15.8504914
            [longitude] => -48.9583997
            [altitude] => 749.0999755859375
            [provider] => fused
            [rssi] => -94
        )

    [22] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 665
            [mac] => 00:0D:18:A1:54:71
            [company] => Mega-Trend Electronics CO., LTD.
            [date_seconds] => 1420083794522
            [timestamp] => 1420083794522
            [latitude] => -15.8504859
            [longitude] => -48.9583969
            [altitude] => 748.7999877929688
            [provider] => fused
            [rssi] => -83
        )

    [23] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 667
            [mac] => 24:C6:96:5E:7B:B9
            [company] => Samsung Electronics Co.,Ltd
            [date_seconds] => 1420083840328
            [timestamp] => 1420083840577
            [latitude] => -15.8502176
            [longitude] => -48.9585146
            [altitude] => 746.5999755859375
            [provider] => fused
            [rssi] => -91
        )

    [24] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 666
            [mac] => 00:54:AF:50:C9:8F
            [company] => Continental Automotive Systems Inc.
            [date_seconds] => 1420083815293
            [timestamp] => 1420083867328
            [latitude] => -15.8501471
            [longitude] => -48.958544
            [altitude] => 749.5999755859375
            [provider] => fused
            [rssi] => -96
        )

    [25] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 668
            [mac] => DC:3E:F8:A7:DE:C7
            [company] => 
            [date_seconds] => 1420083848715
            [title] => Nokia 310
            [timestamp] => 1420083884569
            [latitude] => -15.8500601
            [longitude] => -48.9585936
            [altitude] => 744.0
            [provider] => fused
            [rssi] => -96
        )

    [26] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 656
            [mac] => 00:54:AF:50:5A:CE
            [company] => Continental Automotive Systems Inc.
            [date_seconds] => 1420083232134
            [timestamp] => 1420083961168
            [latitude] => -15.8497313
            [longitude] => -48.9585141
            [altitude] => 751.2999877929688
            [provider] => fused
            [rssi] => -90
        )

    [27] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 673
            [mac] => 80:50:1B:85:5A:ED
            [company] => Nokia Corporation
            [date_seconds] => 1420084029064
            [timestamp] => 1420084078972
            [latitude] => -15.8495164
            [longitude] => -48.9583397
            [altitude] => 751.7999877929688
            [provider] => fused
            [rssi] => -72
        )

    [28] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 671
            [mac] => BC:F5:AC:0B:26:49
            [company] => 
            [date_seconds] => 1420083954108
            [title] => LG-C397
            [timestamp] => 1420084082109
            [latitude] => -15.8495164
            [longitude] => -48.9583397
            [altitude] => 751.7999877929688
            [provider] => fused
            [rssi] => -95
        )

    [29] => Array
        (
            [local_mac] => 98:D6:F7:83:68:39
            [device_type] => 1
            [id] => 672
            [mac] => 88:C9:D0:6B:C1:AA
            [company] => 
            [date_seconds] => 1420083990120
            [title] => LG-E467f
            [timestamp] => 1420084082257
            [latitude] => -15.8495164
            [longitude] => -48.9583397
            [altitude] => 751.7999877929688
            [provider] => fused
            [rssi] => -92
        )

)

$maclistResults数组的示例:

Array
(
    [0] => Array
        (
            [mac] => E8:5B:5B
            [company] => LG ELECTRONICS INC
        )

    [1] => Array
        (
            [mac] => E8:5B:F0
            [company] => Imaging Diagnostics
        )

    [2] => Array
        (
            [mac] => E8:5D:6B
            [company] => Luminate Wireless
        )

    [3] => Array
        (
            [mac] => E8:5E:53
            [company] => Infratec Datentechnik GmbH
        )

    [4] => Array
        (
            [mac] => E8:61:1F
            [company] => Dawning Information Industry Co.,Ltd
        )

    [5] => Array
        (
            [mac] => E8:61:7E
            [company] => Liteon Technology Corporation
        )

    [6] => Array
        (
            [mac] => E8:61:83
            [company] => Black Diamond Advanced Technology, LLC
        )

    [7] => Array
        (
            [mac] => E8:61:BE
            [company] => Melec Inc.
        )

    [8] => Array
        (
            [mac] => E8:66:C4
            [company] => Datawise Systems
        )

    [9] => Array
        (
            [mac] => E8:6D:52
            [company] => ARRIS Group, Inc.
        )

    [10] => Array
        (
            [mac] => E8:6D:54
            [company] => Digit Mobile Inc
        )
)

$maclistResult['mac']字段小于$bluescan['mac'],但前三对在两个数组上匹配,使用substr()

以下代码正在执行我需要的操作,但执行任务需要63秒。

$maclist = 'maclist';
$maclistResults = $db->$maclist->find(array('mac'=>substr($value['mac'], 0, 8)), array('mac'=>1, 'company'=>1));
$maclistResults = iterator_to_array($maclistResults);
foreach ($bluescan as $key => $value)
{
    foreach ($maclistResults as $value2) 
    {
        if (empty($value['company']))
        {
            $bluescan[$key]['company'] = $value2['company'];
        }
    }
}

我想知道如何仅使用一个['company']来更新$bluescan的{​​{1}}值,因为另一个foreach不是最佳值。

2 个答案:

答案 0 :(得分:0)

$ bluescan [$ key] ['company'] = $ value2 ['company'];您应该将$maclistResults数组转换为以下格式:

Array
(
    'E8:5B:5B' => LG ELECTRONICS INC,
    'E8:5B:5A' => SAMSUNG
    ...
)

然后查看它会很快,比如

$bluescan[$key]['company'] = $macResults[$mac];

答案 1 :(得分:0)

因为你只需要与bluescan中的元素一样多的mac开始。

//prepare all macs from bluescan and keep reference to their elements
$macs = array();
foreach ($bluescan as $blueIndex => $blueElement) {
    $macs[substr($blueElement['mac'], 0, 8)][] = $blueIndex;
}

现在$macs是地图mac => [bluescan指数]

//query for maclist
$maclist = query("SELECT mac, company FROM maclist_table WHERE mac IN (" . implode(',', array_keys($macs)) . ")");

//finally assign companies to bluescans
foreach ($maclist as $macElement) {
    foreach ($macs[$macElement['mac']] as $blueIndex) {
        $bluescan[$blueIndex]['company'] = $macElement['company'];
    }
}