我有 2 个数组,需要在第二个数组中找到不在第一个数组中的唯一值,并将其输出到第三个数组。
阵列 1
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
阵列 2
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
输出阵列
$array3 = array(
0 => 'value81',
);
我已经尝试了几种不同的方式array_diff它只给了我一个组合在一起的 array1 和 array2 的数组。
UPDATE:array_diff在上面的示例数组上运行良好,但我无法让它适用于我在下面使用的实际数组。
阵列 1
$array1 = array (
0 => 'e8B283B606A212d82036F74f88177375125440F6',
1 => '633AD0A20588237b6908Ee35B23be06bf5c17b90',
2 => '81C025abAc1a3d51dfcae7a2C226ADD463B794d7',
3 => '1fEc22B25E2200cA88D30B6Ba1d8B5696b2b7D52',
4 => 'AA7A7C2DECB180f68F11E975e6D92B5Dc06083A6',
5 => '09a054B60bd3B908791B55eEE81b515B93831E99',
6 => 'f155825650d85fdc50a2e7bbc27b35b666f4b0a4',
7 => '7e42976faf925dbacedd116a2b6a2bb5c9adef4f',
8 => '556754785B5428cB5C04B798Ea993e5d3c550A66',
9 => '5a4904f474077a021eee8829f7d11d50fe76993f',
10 => '36C39434dB7987Cae67233D06de9427c4C67E787',
11 => '3c1ddb1fb17d83788039d5750c1136017c637a55',
12 => '0f94e2a44ced2a368da8a1edeaa337f13a8560d0',
13 => '05227e4FA98a6415ef1927E902dc781AA7eD518a',
14 => 'EC2E833E90654EE6DD71e7b9EA3bE8B97299CC03',
15 => 'B279F56B1F71F85d59Dc1499E697E4F005298c14',
16 => '73d4F0e3eD1dE3BD278a71C02Adbaf6AD51A610C',
17 => '7D10f0fa8aB734328718212f21Aa8018CCcEd0f4',
18 => 'C9040Ca5e0A7c675343facbD28fe698594168522',
19 => '79F8fFa1502EE1DD6602c2459ECE6960D46012Ac',
20 => '66EA40Bb3F67b8Dc101fB01e6273A4F913Aac559',
21 => 'eC2A9A192929ed1f280f7Fd8935b881e3a649dc0',
22 => 'ba944AbD6Ad261B220dcc8ba6b53410Cb024FCB1',
23 => 'edb477Ca9ADE4d1152666eE914cB279179ffED92',
24 => 'C683946C5dF8eC48D20d64CaF58257b6135faEbB',
25 => '949990EccC1735681C6dFc64516aC882e4948cE2',
26 => '5a6dc9dbce3FdDfDE2905B7ce45b87BFe5bd6872',
27 => '177A56bb19024737C945eb040902fe2124c63fc2',
28 => 'cfd090de8cc555a48b5e742325d88c5937b9074c',
29 => '91671d0C02c7465b0948e819d2C2d261405301cF',
30 => 'A5c5f7fF2186898283e000352770e1cb140d1a00',
31 => 'Db5a91E6f925A22FE995BfB403eacc74DFE34F04',
32 => '26d13C17659e1305344eE31635B5A2b72e94d82E',
33 => '136f4b5b6a306091b280e3f251fa0e21b1280cd5',
34 => '4cdc990d2EE172534d503Fec256B16B5fD09394f',
35 => '04EeA222B3CC6F1F667915E952539eCa8468b3FE',
);
阵列 2
$array2 = array (
0 => '28697b4a1618a77b1d61a85e99174616b519f629',
1 => 'e8b283b606a212d82036f74f88177375125440f6',
2 => '633ad0a20588237b6908ee35b23be06bf5c17b90',
3 => '81c025abac1a3d51dfcae7a2c226add463b794d7',
4 => 'fead04e9b311eebdc5f1e963b7472af15b21430d',
5 => '23d3a453c7bd4634b81219d63b12d5e3ceec04f6',
6 => '716f412c236236845d8592873bd1e78d024fadf5',
7 => '460eebde20336aa615370f4b7b7a916e5656d4e6',
8 => '5ff58e03ac620166f6093f85c2edec521fac7a26',
9 => 'f155825650d85fdc50a2e7bbc27b35b666f4b0a4',
10 => '556754785b5428cb5c04b798ea993e5d3c550a66',
11 => '5a4904f474077a021eee8829f7d11d50fe76993f',
12 => 'e8577ff0c2a982b1550993a40c34cf26ad4732ff',
13 => '36c39434db7987cae67233d06de9427c4c67e787',
14 => '1fec22b25e2200ca88d30b6ba1d8b5696b2b7d52',
15 => '7e42976faf925dbacedd116a2b6a2bb5c9adef4f',
16 => '3c1ddb1fb17d83788039d5750c1136017c637a55',
17 => 'd7644fa6fe75eb7338c46807a640fa35b2c6dd12',
18 => '9175e0e8434cb6c5cc45a64f8a80f66ec81b47bf',
19 => '91c700c233491fad57ee7b91e0a5de5cbbb63080',
20 => 'ec2e833e90654ee6dd71e7b9ea3be8b97299cc03',
21 => '73d4f0e3ed1de3bd278a71c02adbaf6ad51a610c',
22 => '6522bbd602e1cdb3c1e12a04c8a5d1a9f5352a1e',
23 => '92598e499f4c576104105279cf926df447f2d044',
24 => 'bb826a44f1c8069207248d34a56ce05bc72d2901',
25 => '66ea40bb3f67b8dc101fb01e6273a4f913aac559',
26 => '7d10f0fa8ab734328718212f21aa8018ccced0f4',
27 => 'f7435817b7d4165cbf7b56539426267cd8872895',
28 => '73ddf33aff4274dc706241c4a1263196e5e6e872',
29 => 'edb477ca9ade4d1152666ee914cb279179ffed92',
30 => 'c683946c5df8ec48d20d64caf58257b6135faebb',
31 => '949990eccc1735681c6dfc64516ac882e4948ce2',
32 => '5a6dc9dbce3fddfde2905b7ce45b87bfe5bd6872',
33 => 'f0e6f7b1a9e917cdd76981b20265719c0b96875c',
34 => 'a7c4cea7f9263ceabb958118636929521b61ec80',
35 => '2ad41576364037cf25fd08948f425b63610d6cf3',
36 => 'cc73a3c13fe628823b6759ad07199794a252a41d',
37 => 'b30a9afb76e4ba949f5523a28bda7ade35e6f5d0',
38 => '650ff5efc2f881236d3079e465cc3e087c6ddca5',
39 => '8dc82ddbf1efb4befcb577b1c0a6efb812a0c562',
40 => '1b2a811c808d30aa2a0270b6729609a84a6b9e82',
41 => '442b7055dd9b2c483884ad250734172b2ae7fc76',
42 => 'c9040ca5e0a7c675343facbd28fe698594168522',
43 => 'e32bb01574b9a36bf16884cc7160977efc9dd6b3',
44 => 'caad2020967f0f314fb8a150413f7f9fc26c0f73',
45 => 'ad4273e44ce8896c943ee6851d96a307b14205e5',
46 => '6f0784344fe67eaf46d74171f5469274b690648f',
47 => '81036acab4da3d559fe11299d48d43b3d31551a7',
48 => '0f94e2a44ced2a368da8a1edeaa337f13a8560d0',
);
法典:
$result = array_diff($array2, $array1);
echo '<pre>' . var_export($result, true) . '</pre><br>';
您是否尝试通过交换参数来使用array_diff()
或array_diff_assoc()
?
<?php
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
$result=array_diff_assoc($array2,$array1);
print_r($result);
?>
工作演示:https://3v4l.org/aNjUm
您可以通过array_diff
<?php
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
$value = array_diff($array2, $array1);
$array3[] = $value[3];
print_r($array3);
?>
输出
Array
(
[0] => value81
)
注意:如果您交换函数array_diff
参数,您将获得差异结果,如下所示
1:
$value = array_diff($array2, $array1);
//Output
Array
(
[0] => value81
)
阿拉伯数字:
$value = array_diff($array1, $array2);
//Output
Array
(
[0] => value79
)
问题是我在问题更新中发布的实际数组中区分大小写。
这就是最终对我有用的东西。我使用了带有strcasecmp
回调函数的array_udiff
。这给了我想要的结果。
strcasecmp
忽略大小写,将 A 视为 A 或将 B 视为 b,反之亦然。
$result = array_udiff($array2, $array1, 'strcasecmp');