Comparing 2 arrays and modifying 1
Ok, assume that I have 2 arrays.
$myArray = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
$badNumbers = ('3', '6', '10')
What I want to do is开发者_C百科 compare $myArrays
with $badNumbers
, then modify $myArrays
to remove anything found in the $badNumbers
.
So after some code, the end result would be:
$myArray = ('1', '2', '4', '5', 7', '8', '9');
$badNumbers = ('3', '6', '10')
Is there anyway to do this? I've of some things, but nothing seems to work. The comparing part alone I already have some problems.
EDIT: I'm fine with a third array too. Something along the line of for each value, if it doesn't appear in the second array, array_push
to the new array. But I'm still not so sure on how to do this.
You can use array_diff
to get the result.
$myArray= array('1','2','3','4','5','6','7','8','9','0');
$badNumbers= array('3','6','0');
$available = array_diff($myArray, $badNumbers);
print_r($available);
echo '<br /><br />' . implode(', ', $available);
Hope this helps.
$result = array_diff($myArray, $badNumbers);
echo count($result) ? 'there were differences' : 'there werent';
I suppose, array_diff() function does what you want.
What does "count" do? Is "echo" like "print"?
echo count($result) ? 'there were differences' : 'there werent';
精彩评论