我想检查下面数组的2个值,并将数组B 上的值替换为0或1值(如果每个数组值与数组A 匹配或不匹配)。 这是我的数组数据:
数组A :
Array (
[a1] => 2
[a2] => 2
[a3] => 3 )
数组B :
Array (
[0] => Array
(
[a1] => 2
[a2] => 1
[a3] => 3
[so] => 7
)
[1] => Array
(
[a1] => 3
[a2] => 2
[a3] => 2
[so] => 8
)
[2] => Array
(
[a1] => 1
[a2] => 3
[a3] => 4
[so] => 9
) )
这是我期望的输出:
知道如何在PHP中执行此操作吗?因为我仍然困惑如何将array_diff或array_intersect与foreach循环结合起来
答案 0 :(得分:1)
也许不是最优雅的解决方案,但它确实有效:
$a = array
(
'a1' => 2,
'a2' => 2,
'a3' => 3
);
$b = array (
array
(
'a1' => 2,
'a2' => 1,
'a3' => 3,
's0' => 7
),
array
(
'a1' => 3,
'a2' => 2,
'a3' => 2,
's0' => 8
),
array
(
'a1' => 1,
'a2' => 3,
'a3' => 4,
's0' => 9
) );
$c = array ();
$i=0;
foreach ($b as $b_row)
{
$c[$i]['a1'] = ( $a['a1'] == $b_row['a1'] ) ? 1 : 0;
$c[$i]['a2'] = ( $a['a2'] == $b_row['a2'] ) ? 1 : 0;
$c[$i]['a3'] = ( $a['a3'] == $b_row['a3'] ) ? 1 : 0;
$c[$i]['s0'] = $b_row['s0'];
$i++;
}
或更紧凑:
$i=0;
foreach ($b as $b_row)
{
foreach ($b_row as $b_cell => $value)
{
if (isset ($a[$b_cell]))
{
$c[$i][$b_cell] = ( $a[$b_cell] == $value ) ? 1 : 0;
}
else
{
$c[$i][$b_cell] = $value;
}
}
$i++;
}
答案 1 :(得分:0)
现在无法测试,但它可能是这样的
noexcept(std::is_nothrow_constructible<T>::value)