PHP数组_intersect()-它如何处理不同的类型?
如果我有一个基本上是各种数字的零填充字符串表示的值数组和另一个整数数组,那么数组_intersect是否仍然匹配不同类型的元素 例如,这是否有效:PHP数组_intersect()-它如何处理不同的类型?,php,arrays,casting,types,array-intersect,Php,Arrays,Casting,Types,Array Intersect,如果我有一个基本上是各种数字的零填充字符串表示的值数组和另一个整数数组,那么数组_intersect是否仍然匹配不同类型的元素 例如,这是否有效: $arrayOne = array('0003', '0004', '0005'); $arrayTwo = array(4, 5, 6); $intersect = array_intersect($arrayOne, $arrayTwo); // $intersect would then be = "array(4, 5)" 如果没有,实
$arrayOne = array('0003', '0004', '0005');
$arrayTwo = array(4, 5, 6);
$intersect = array_intersect($arrayOne, $arrayTwo);
// $intersect would then be = "array(4, 5)"
如果没有,实现这一目标最有效的方法是什么?只需循环并比较,或者循环并将所有内容转换为整数,然后运行array_intersect?$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
排列
[1] => 4
[2] => 5
$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
排列
[1] => 4
[2] => 5
来自:
在您的示例中,$intersect将是一个空数组,因为5!==005和4!==004来自:
在您的示例中,$intersect将是一个空数组,因为5!==005和4!==004我认为应该提到string5==string5。我认为应该提到string5==string5。
Note: Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2.
In words: when the string representation is the same.