我想计算不同的两个数组,但出现错误;
Notice: Array to string conversion in x.php on line 255
Run Code Online (Sandbox Code Playgroud)
并没有计算出不同。
码:
$db->where('lisansID', $_POST['licence']);
$mActivation = $db->get('moduleactivation', null, 'modulID');
$aktifler = Array();
$gelenler = Array();
foreach($mActivation as $key=>$val)
{
$aktifler[] = $val;
}
foreach ($_POST['module'] as $key => $value) {
$gelenler[] = $val;
}
echo '<pre>Aktifler: ';
print_r($aktifler);
echo '</pre>';
echo '<pre> Gelenler:';
print_r($gelenler);
echo '</pre> Fark:';
///line 255:
var_dump(array_diff($aktifler, $gelenler));
Run Code Online (Sandbox Code Playgroud)
array_diff只能比较可以转换为的字符串或值(string)。但是$aktiflerand 的元素$gelenler本身就是数组,这就是为什么您会收到此通知(此外,将数组转换为字符串始终会导致字符串“ Array”,因此所有数组将被视为相等)。
注意:
当且仅当(string)$ elem1 ===(string)$ elem2时,两个元素才视为相等。换句话说:当字符串表示相同时。
请array_udiff改用,您可以在其中定义自己的比较功能。
$out = array_udiff($aktifler, $gelenler, function($a, $b) {
// the callback must return 0 for equal values
return intval($a != $b);
});
Run Code Online (Sandbox Code Playgroud)