我试图弄清楚如何检查数组中的重复项,但以下代码没有给出预期的结果:
for ($i = 0; $i < 10; $i++) {
for ($j = 0; $j < count($ships[$i][0]); $j++) {
if ($hitX.$hitY === $ships[$i][1][$j].$ships[$i][0][$j]) {
echo("hit");
array_push( $ships[$i]["hits"], array($hitX,$hitY));
$ships[$i]["hits"] = array_unique($ships[$i]["hits"])
例如,我想减少以下数组,使其仅包含唯一值:
[hits] => Array
(
[0] => Array
(
[0] => k
[1] => 6
)
[1] => Array
(
[0] => j
[1] => 6
)
[2] => Array
(
[0] => j
[1] => 6
)
[3] => Array
(
[0] => j
[1] => 6
)
[4] => Array
(
[0] => j
[1] => 6
)
)
)