我必须比较两个7维数组,并且对于每个匹配,我必须将+1计数到一个新数组。
这是带有用户输入的数组,我想与"正确的值进行比较"
Array([task] => Array(
[OF] => Array(
[nr] => Array(
[1] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[2] => Array(
[pos] => Array(
[0] => Array(
[value] => +
)
)
)
[3] => Array(
[pos] => Array(
[1] => Array(
[value] => -
)
[2] => Array(
[value] => +
)
)
)
[4] => Array(
[pos] => Array(
[0] => Array(
[value] => -
)
)
)
[5] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
[6] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[7] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
)
)
),
[TA] => Array(
[nr] => Array(
[1] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[2] => Array(
[pos] => Array(
[0] => Array(
[value] => +
)
)
)
[3] => Array(
[pos] => Array(
[1] => Array(
[value] => -
)
[2] => Array(
[value] => +
)
)
)
[4] => Array(
[pos] => Array(
[0] => Array(
[value] => -
)
)
)
[5] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
[6] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[7] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
)
)
)
正确的值具有相同的结构,具有不同的值。
现在我必须遍历数组并检查用户输入是否= ="正确的值"。
很抱歉,如果难以理解的话。这有点难以解释。
感谢所有的帮助,谢谢大家。
编辑://我想知道'任务的正确值的数量'数组例如OF => 12,TA => 3
答案 0 :(得分:0)
如果我理解得很清楚,你需要的是递归比较。这是我的特殊代码,我没有测试它:
function compareArrays($array1, $array2) {
$retval = 0;
for($i = 0; $i < count($array1); $i++) {
// The values are arrays, we need a recursive comparation again
if (is_array($array1[$i]) && is_array($array2[$i])) {
$retval += compareArrays($array1[$i], $array2[$i]);
// The values are equal, we can increase the counter
} else if ($array1[$i] === $array2[$i]) {
$retval++;
// The values are different
} else {
// Is there anything to do?
}
}
return $retval;
}