我有一个名为$json
的数组,有47个子数组。在这些子数组中,有一个[company]
字段。其中15个子阵列有一个空的[company]
字段。
因此,要填充这些空的[company]
字段,我会使用网站根据[mac]
字段搜索公司名称。以下丑陋的黑客完成了工作:
foreach ($json as $key=>$field)
{
if (empty($field['company']))
{
$emptyCompany[] = $field['company'];
$companyFinder = file_get_contents('http://www.adminsub.net/mac-address-finder/'.$field['mac']);
$html = htmlspecialchars($companyFinder);
$cutFrom = "www.google.com/search?q=";
$cutTo = """;
$sub = substr($html, strpos($html, $cutFrom) + strlen($cutFrom), strlen($html));
$newCompany[] = substr($sub, 0, strpos($sub, $cutTo));
}
}
以下是相关阵列。
使用print_r($emptyCompany)
清空公司数组,仅供参考:
Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
使用print_r($newCompany)
的新公司阵列:
Array
(
[0] => Sony Mobile Communications AB
[1] => Microsoft Corporation
[2] =>
[3] => Samsung Electronics Co.,Ltd
[4] =>
[5] => ALPS ERECTORIC CO.,LTD.
[6] => Samsung Electronics Co.,Ltd
[7] => Samsung Electronics Co.,Ltd
[8] => Nokia Corporation
[9] => LG Electronics
[10] => LG Electronics
[11] => Samsung Electronics Co.,Ltd
[12] =>
[13] => LG Electronics
[14] => LG Electronics
)
$json
数组:
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
)
[30] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 674
[mac] => E0:A6:70:1B:CC:AB
[company] => Nokia Corporation
[date_seconds] => 1420084204715
[timestamp] => 1420084204715
[latitude] => -15.849459
[longitude] => -48.957572
[altitude] => 744.5999755859375
[provider] => fused
[rssi] => -86
)
[31] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 675
[mac] => 08:37:3D:66:1B:13
[company] => Samsung Electronics Co.,Ltd
[date_seconds] => 1420084275075
[title] => GT-I8190L
[timestamp] => 1420084275075
[latitude] => -15.8496899
[longitude] => -48.9572679
[altitude] => 755.5999755859375
[provider] => fused
[rssi] => -93
)
[32] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 676
[mac] => 78:47:1D:B6:A0:16
[company] => Samsung Electronics Co.,Ltd
[date_seconds] => 1420084287659
[title] => Angelica
[timestamp] => 1420084292776
[latitude] => -15.8497522
[longitude] => -48.9572883
[altitude] => 755.4000244140625
[provider] => fused
[rssi] => -98
)
[33] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 677
[mac] => C8:AA:21:20:A6:6E
[company] => Motorola Mobility, LLC.
[date_seconds] => 1420084455249
[title] => Motorola EX108
[timestamp] => 1420084491070
[latitude] => -15.8502285
[longitude] => -48.9576278
[altitude] => 753.2000122070313
[provider] => fused
[rssi] => -90
)
[34] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 670
[mac] => 58:A2:B5:28:3C:4C
[company] => LG Electronics
[date_seconds] => 1420083891600
[title] => LG-A395
[timestamp] => 1420084511148
[latitude] => -15.8502971
[longitude] => -48.9577257
[altitude] => 754.5
[provider] => fused
[rssi] => -91
)
[35] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 678
[mac] => 00:1F:89:88:8C:2E
[company] => Signalion GmbH
[date_seconds] => 1420084568312
[timestamp] => 1420084568312
[latitude] => -15.8503558
[longitude] => -48.9577714
[altitude] => 754.0
[provider] => fused
[rssi] => -87
)
[36] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 679
[mac] => 10:F9:6F:FF:5A:54
[company] => LG Electronics
[date_seconds] => 1420084666382
[timestamp] => 1420084666382
[latitude] => -15.8507209
[longitude] => -48.9580627
[altitude] => 747.7000122070313
[provider] => fused
[rssi] => -99
)
[37] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 662
[mac] => 60:A1:0A:6B:7D:C0
[company] => Samsung Electronics Co.,Ltd
[date_seconds] => 1420083732890
[title] => Angela
[timestamp] => 1420084685321
[latitude] => -15.8507479
[longitude] => -48.9581244
[altitude] => 747.2000122070313
[provider] => fused
[rssi] => -81
)
[38] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 680
[mac] => FC:19:10:5A:19:E8
[company] =>
[date_seconds] => 1420084759306
[title] => SM-G355M
[timestamp] => 1420084809467
[latitude] => -15.8509884
[longitude] => -48.9583195
[altitude] => 748.7999877929688
[provider] => fused
[rssi] => -93
)
[39] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 669
[mac] => 39:2E:EA:30:62:50
[company] =>
[date_seconds] => 1420083878124
[title] => P200
[timestamp] => 1420084905369
[latitude] => -15.8513452
[longitude] => -48.9586177
[altitude] => 745.7999877929688
[provider] => fused
[rssi] => -92
)
[40] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 682
[mac] => F0:5A:09:DC:A4:4A
[company] => Samsung Electronics Co.,Ltd
[date_seconds] => 1420085011925
[title] => GT-P3100
[timestamp] => 1420085011925
[latitude] => -15.8520308
[longitude] => -48.9589976
[altitude] => 756.5
[provider] => fused
[rssi] => -92
)
[41] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 681
[mac] => BC:F5:AC:8A:DB:85
[company] =>
[date_seconds] => 1420084909871
[title] => LG-A395
[timestamp] => 1420085043013
[latitude] => -15.8522648
[longitude] => -48.9590723
[altitude] => 759.7000122070313
[provider] => fused
[rssi] => -70
)
[42] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 683
[mac] => 74:5E:1C:54:B5:3E
[company] => PIONEER CORPORATION
[date_seconds] => 1420085184840
[title] => MVH-X368BT
[timestamp] => 1420085184840
[latitude] => -15.8525656
[longitude] => -48.9586717
[altitude] => 761.7000122070313
[provider] => fused
[rssi] => -91
)
[43] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 684
[mac] => 00:19:C1:99:3D:6F
[company] => Alps Electric Co., Ltd
[date_seconds] => 1420085492235
[timestamp] => 1420085492235
[latitude] => -15.8526307
[longitude] => -48.9585812
[altitude] => 765.4000244140625
[provider] => fused
[rssi] => -97
)
[44] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 685
[mac] => 00:23:3D:4D:41:07
[company] => Novero holding B.V.
[date_seconds] => 1420085591500
[timestamp] => 1420085591500
[latitude] => -15.8526308
[longitude] => -48.9585814
[altitude] => 765.4000244140625
[provider] => fused
[rssi] => -91
)
[45] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 686
[mac] => 90:03:B7:CE:7C:64
[company] => PARROT
[date_seconds] => 1420087671133
[title] => AVH-X7580BT
[timestamp] => 1420089145817
[latitude] => -15.8527276
[longitude] => -48.9585511
[altitude] => 765.5
[provider] => fused
[rssi] => -96
)
[46] => Array
(
[local_mac] => 98:D6:F7:83:68:39
[device_type] => 1
[id] => 644
[mac] => 48:59:29:5B:5D:89
[company] =>
[date_seconds] => 1420078339246
[title] => LG-A395
[timestamp] => 1420090390755
[latitude] => -15.8526706
[longitude] => -48.9585857
[altitude] => 764.5
[provider] => fused
[rssi] => -92
)
)
我的问题是:如何正确地将$newCompany
数组中的新公司值添加到$json
数组?。我尝试使用push_array()
和push_merge()
,但我无法使用它。
答案 0 :(得分:2)
您可以直接在循环中添加值
$json[$key]['company'] = substr($sub, 0, strpos($sub, $cutTo));
如果无法做到这一点,请确保您的$newCompany
- 数组已编入$key
索引,然后合并数组:
// Option 1 - use this line in the loop and then loop through the created array
$newCompany[$key] = substr($sub, 0, strpos($sub, $cutTo));
foreach($newCompany as $key => $value){
$json[$key]['company'] = $value;
}
// Option 2 - use this line in the loop and use array_merge_recursive
$newCompany[$key] = ['company' => substr($sub, 0, strpos($sub, $cutTo))];
array_merge_recursive($json, $newComany);
我不完全确定array_merge_recursive将如何处理它。在使用之前,你一定要测试一下!