我有两个数组:
$ ARRAY1:
Array
(
[0] => 2032
[1] => 2027
[2] => 2025
[3] => 2023
[4] => 2021
[5] => 2018
[6] => 2014
[7] => 2011
[8] => 2009
[9] => 1947
[10] => 1941
[11] => 1939
[12] => 1937
[13] => 1935
[14] => 1933
[15] => 1928
[16] => 1926
[17] => 1924
[18] => 1922
[19] => 1920
[20] => 1918
[21] => 1916
[22] => 1910
[23] => 1881
[24] => 1680
[25] => 1678
[26] => 1879
[27] => 1877
[28] => 1875
[29] => 1873
[30] => 1871
[31] => 1869
[32] => 1865
[33] => 1863
[34] => 1858
[35] => 1850
[36] => 1844
[37] => 1838
[38] => 1829
[39] => 1827
[40] => 1825
[41] => 1821
[42] => 1819
[43] => 1814
[44] => 1812
[45] => 1810
[46] => 1808
[47] => 1806
[48] => 1804
[49] => 1801
[50] => 1793
[51] => 1791
[52] => 1789
[53] => 1784
[54] => 1774
[55] => 1772
[56] => 1770
[57] => 1768
[58] => 1766
[59] => 1764
[60] => 1762
[61] => 1760
[62] => 1754
[63] => 1748
[64] => 1746
[65] => 1744
[66] => 1740
[67] => 1738
[68] => 1732
[69] => 1722
[70] => 1720
[71] => 1716
[72] => 1714
[73] => 1711
[74] => 1708
[75] => 1703
[76] => 1699
[77] => 1673
[78] => 1671
[79] => 1669
[80] => 1667
[81] => 1665
[82] => 1663
[83] => 1661
[84] => 1659
[85] => 1655
)
$数组2:
Array
(
[0] => 1665
[1] => 1671
[2] => 1714
[3] => 1716
[4] => 1722
[5] => 1732
[6] => 1774
[7] => 1801
[8] => 1804
[9] => 1916
[10] => 1918
[11] => 1920
[12] => 1924
[13] => 1933
[14] => 1935
[15] => 1939
)
我需要做的是检查数组1的每个值并查看它是否在数组2中,如果值在数组2中,则将该值放入数组3中。但是数组3需要保持值在数组1中出现的顺序。
我可以编写一些代码来循环遍历数组,一个执行in_array(),如果是,则删除数组三。这可行,但我的问题的原因是,是否有任何功能已经可用于此任务?如果没有,那么最有效率和最快速的方法是什么呢?
答案 0 :(得分:16)
$array3 = array_intersect($array, $array2)
答案 1 :(得分:5)
你可以使用array_intersect()。