[英]Comparing two and three arrays for the same values contained anyhere within the array
I have 3 arrays.我有 3 个 arrays。
$eldest_range
$second_range
$third_range
$eldest_range
$second_range
$third_range
$eldest_range = range($start_year1,$end_year1);
print_r($eldest_range);
$second_range = range($start_year2,$end_year2);
print_r($second_range);
$third_range = range($start_year3,$end_year3);
print_r($third_range);
Which returns: <br> Array ( [0] => 2030 [1] => 2031 [2] => 2032 [3] => 2033 [4] => 2034 [5] => 2035 ) <Br> Array ( [0] => 2032 [1] => 2033 [2] => 2034 [3] => 2035 [4] => 2036 [5] => 2037 ) <br> Array ( [0] => 2034 [1] => 2035 [2] => 2036 [3] => 2037 [4] => 2038 [5] => 2039 ) <Br>
返回:
<br> Array ( [0] => 2030 [1] => 2031 [2] => 2032 [3] => 2033 [4] => 2034 [5] => 2035 ) <Br> Array ( [0] => 2032 [1] => 2033 [2] => 2034 [3] => 2035 [4] => 2036 [5] => 2037 ) <br> Array ( [0] => 2034 [1] => 2035 [2] => 2036 [3] => 2037 [4] => 2038 [5] => 2039 ) <Br>
I want compare these arrays.我想比较这些 arrays。 To check what if any of them are present within one another.
检查它们中的任何一个是否存在于彼此中。 So in the above case 1 & 2 share 2032, but 1,2 & 3 share 2034.
因此,在上述情况下,1 和 2 共享 2032,但 1,2 和 3 共享 2034。
I would rather check them separately in pairs and then as a three.我宁愿成对检查它们,然后作为三个检查。 (1 & 2 - 1 & 3 - 2 & 3 - 1, 2 & 3)
(1 & 2 - 1 & 3 - 2 & 3 - 1, 2 & 3)
Here's what I have tried.这是我尝试过的。 This is for Eldest to second:
这是给老二的:
$result=array_intersect_assoc($eldest_range, $second_range);
print_r($result);
which returns, nothing: array()
and I have also tried..返回,什么都没有:
array()
我也试过了..
$result=array_intersect_key($eldest_range, $second_range);
print_r($result);
which returns just the whole of $eldest_range
它只返回整个
$eldest_range
Array ( [0] => 2030 [1] => 2031 [2] => 2032 [3] => 2033 [4] => 2034 [5] => 2035 )
Am i using the wrong array_intersect()
.我是否使用了错误的
array_intersect()
。 I just want to see any values that are in 2 arrays.我只想查看 2 arrays 中的任何值。 and 3 arrays (as above).
和 3 arrays(如上)。
Thank you谢谢
Sometimes it is very useful to read a manual , which says that array_intersect_assoc
有时阅读手册非常有用,其中说
array_intersect_assoc
Computes the intersection of arrays with additional index check
通过附加索引检查计算 arrays 的交集
Do you need this index check?你需要这个索引检查吗? Definitely no .
绝对没有。
What you need is simple array_intersect
:你需要的是简单的
array_intersect
:
$eldest_range = range(2030, 2035);
$second_range = range(2032, 2037);
$third_range = range(2034, 2039);
print_r(array_intersect($eldest_range, $second_range)); // [2032, 2033, 2034, 2035]
print_r(array_intersect($eldest_range, $second_range, $third_range)); // [2034, 2035]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.