Reputation: 4557
I have two arrays
$give_ans = Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 1
[7] => 2
[8] =>
[9] => 4
[10] => 3
[11] => 1
[12] => 0
[13] => 2
[14] => 1
[15] => 2
[16] => 0
[17] => 0
)
and
$correct_Ans = Array (
[0] => 3
[1] => 1
[2] => 4
[3] => 2
[4] => 3
[5] => 2
[6] => 1
[7] => 2
[8] => 2
[9] => 2
[10] => 2
[11] => 2
[12] => 2
[13] => 2
[14] => 2
[15] => 2
[16] => 2
[17] => 2
)
From these two array, I want to check that if the elements of the same index of the both array are equal then they should be put into one array.
If the element in give_ans
array is 0 then they should be put into another array
and the remaining elements should be put into the third array.
How can I achieve this please help me ?
Upvotes: 0
Views: 138
Reputation: 771
$given_ans = array(0 => 0, 1 => 0,2 =>0 , 3 =>0 ,4 => 0, 5 =>0 , 6 =>1 ,7 =>2 ,8 => 1,9 =>4 , 10 => 3,11 =>1 ,12 => 0,13 => 2,14 => 1,15 =>2 ,16 =>0 ,17 =>0 );
$correct_ans = array(0 => 3 ,1 => 1 ,2 => 4 ,3 => 2 ,4 => 3 ,5 => 2 ,6 => 1 ,7 => 2 ,8 => 2 ,9 => 2 ,10 => 2 ,11 => 2 ,12 => 2 ,13 => 2 ,14 => 2 ,15 => 2 ,16 => 2 ,17 => 2 );
$count = count($given_ans);
for ($i = 0; $i < $count; $i++) {
if ($given_ans[$i] == 0) {
$zero_ans[$i] = $given_ans[$i];
unset($given_ans[$i]);
} else if ($given_ans[$i] == $correct_ans[$i]) {
$result_ans[$i] = $given_ans[$i];
unset($given_ans[$i]);
}
}
echo "Zero value:";
print_r($zero_ans);
echo "Correct:";
print_r($result_ans);
echo "Remaining:";
print_r($given_ans);
Here working: link
$result_ans will now contain both key and value of matching indexes/values; $zero_ans will contain $given_ans 0 values; $given_ans will contain remaining values.
If you don't want $given_ans to be erased, you can always make a copy of the array.
Upvotes: 0
Reputation: 4416
$newArray = array();
$nullArray = array();
$tempArray = array();
for ($i = 0; $i < count($give_ans) ; $i++){
if (($give_ans[$i] === $correct_ans[$i]) && $give_ans !== 0){
array_push($newArray, $give_ans[$i]);
array_push($tempArray, $i);
}else if ($give_ans[$i] === 0){
array_push($nullArray, $i);
array_push($tempArray, $i);
}
}
$othersArray = array();
for ($i = 0; $i < count($give_ans); $i++){
if ($tempArray[$i] !== $give_ans[$i])
array_push($othersArray, $give_ans[$i];
}
for ($i = 0; $i < count($correct_ans); $i++){
if ($tempArray[$i] !== $correct_ans[$i])
array_push($othersArray, $correct_ans[$i];
}
I've got no time to test it ;)
Upvotes: 1
Reputation: 3647
$length = count($given_ans);
$result = array();
for($i=0; $i < $length; $i++) {
if ($given_ans[$i] == $correct_Ans[$i]) {
$result[] = $given_ans[$i];
}
}
Working demo https://eval.in/86693
Upvotes: 1
Reputation: 327
You can use this logic
$result = array();
foreach($give_ans as $key => $value){
if($give_ans[$key] == $correct_Ans[$key]){
$result[$key] = $value;
}
}
print_r($result);
Upvotes: 1
Reputation: 11
$give_ans = array(0 => 0, 1 => 0,2 =>0 , 3 =>0 ,4 => 0, 5 =>0 , 6 =>1 ,7 =>2 ,8 => 1,9 =>4 , 10 => 3,11 =>1 ,12 => 0,13 => 2,14 => 1,15 =>2 ,16 =>0 ,17 =>0 );
$correct_Ans = Array (0 => 3 ,1 => 1 ,2 => 4 ,3 => 2 ,4 => 3 ,5 => 2 ,6 => 1 ,7 => 2 ,8 => 2 ,9 => 2 ,10 => 2 ,11 => 2 ,12 => 2 ,13 => 2 ,14 => 2 ,15 => 2 ,16 => 2 ,17 => 2 );
print_r(array_diff($give_ans, $correct_Ans));
Returns an array of elements that don't match
Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [12] => 0 [16] => 0 [17] => 0 )
Upvotes: 1
Reputation: 2783
function array_diff will help you here:
http://de3.php.net/manual/en/function.array-diff.php
But you want to index all the duplicate contents so simply do for loop:
$equalResult = array();
for($c = 0; $c < count($give_ans); $c++) {
if(isset($give_ans[$c]) && isset($correct_Ans[$c])) {
if($give_ans[$c] == $correct_Ans[$c]) {
$equalResult[] = array($give_ans[$c] => $correct_Ans[$c]);
}
}
}
Upvotes: 1