Array 1
Array
(
[0] => Array
(
[id] => 4083
[player_name] => AD373
)
[1] => Array
(
[id] => 4084
[player_name] => SM8
)
[2] => Array
(
[id] => 4085
[player_name] => WW8
)
)
Array 2
Array
(
[0] => Array
(
[id] => 4046
[player_name] => AD373
)
[1] => Array
(
[id] => 4080
[player_name] => WW8
)
[2] => Array
(
[id] => 4045
[player_name] => 8799
)
[3] => Array
(
[id] => 4060
[player_name] => SM8
)
)
mycode
foreach ($array1 as $gkey => $gvalue) {
$notexist = array();
foreach ($array2 as $smkey => $smvalue) {
if ($smvalue['player_name'] !== $gvalue['player_name']) {
$notexist[] = $smvalue;
}
}
}
Question: There will have 2 array, I want to compare this 2 array and find out the value that only appear in one array which is value player_name 8799. But looks like my code will store the wrong result. Does anyone can help on this ya :( ?
CodePudding user response:
Try this:
<?php
$array1 = Array(
0 => Array
(
'id' => 4083,
'player_name' => 'AD373'
),
1 => Array
(
'id' => 4084,
'player_name' => 'SM8'
),
2 => Array
(
'id' => 4085,
'player_name' => 'WW8'
)
);
$array2 = Array(
0 => Array
(
'id' => 4046,
'player_name' => 'AD373'
),
1 => Array
(
'id' => 4080,
'player_name' => 'WW8'
),
2 => Array
(
'id' => 4045,
'player_name' => 8799
),
3 => Array
(
'id' => 4060,
'player_name' => 'SM8'
)
);
foreach($array1 as $value){
$temp1[] = $value['player_name'];
}
foreach($array2 as $value){
$temp2[] = $value['player_name'];
}
$differences = array_merge(array_diff($temp1,$temp2), array_diff($temp2,$temp1));
print_r($differences);
Output:
Array
(
[0] => 8799
)
CodePudding user response:
look at this example
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>