PHP array_intersect() - 它如何处理不同类型? [英] PHP array_intersect() - how does it handle different types?
问题描述
如果我有一个值的数组,基本上是各种数字的字符串表示形式和另一个整数数组,array_intersect()仍然匹配不同类型的元素?
If I've got an array of values that are basically zerofilled string representations of various numbers and another array of integers, will array_intersect() still match elements of different types?
例如,这将工作:
$arrayOne = array('0003', '0004', '0005');
$arrayTwo = array(4, 5, 6);
$intersect = array_intersect($arrayOne, $arrayTwo);
// $intersect would then be = "array(4, 5)"
...如果没有,什么是最有效的方法来完成这个?只是循环和比较,或循环并将一切转换为整数,并运行array_intersect()之后,或...
... and if not, what would be the most efficient way to accomplish this? Just loop through and compare, or loop through and convert everything to integers and run array_intersect() after, or ...
推荐答案
$ cat> test.php
$ cat > test.php
<?php
$arrayOne = array('0003', '0004', '0005');
$arrayTwo = array(4, 5, 6);
$intersect = array_intersect($arrayOne, $arrayTwo);
print_r($intersect );
?>
$ php test.php
$ php test.php
(
)
Array ( )
$
但如果您添加
foreach($arrayOne as $key => $value)
{
$arrayOne[$key] = intval($value);
}
您将获得
$ php test.php
$ php test.php
数组
(
[1] => 4
[2] => 5
)
Array ( [1] => 4 [2] => 5 )
这篇关于PHP array_intersect() - 它如何处理不同类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!