如果不在另一个数组中,则从数组中删除值,但保持相同顺序。

7

I have two arrays:

$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
)

$array2:

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
)

我需要做的是检查数组一中的每个值,并查看它是否在数组二中,如果该值在数组二中,则将该值放入数组三。但是,数组三需要保持数组一中的值出现的顺序。
我可以编写一些代码来循环遍历数组一并执行in_array(),如果返回true则将其插入到数组三中。这样也能实现,但我提出问题的原因是:是否已经有函数可以完成此任务?如果没有,那么最有效和快速的方法是什么?
2个回答

20

4

这看起来像是另一个答案的完全重复(而那个答案是先发布的)。 - Ian Dunn
@IanDunn 哈哈,只相差两分钟。也许他在另一个帖子发布的时候还在撰写这篇文章呢。并不是一个小时之后才发出来的。 :) - Panama Jack
是的,完全可以理解,我并不是说他是有意为之。无论如何,我们不需要两个回答说同样的事情。在我看来,这一个应该被删除,因为它没有添加任何内容。 - Ian Dunn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接