at first I wanna say that I'm new in PHP.
I have an implementation that checks an object is in array or not, if not adds another array. But it always returns false and adds in theorder array.
How can I solve it?
Here part of the code:
$temp = new tempClass($x, $y);
if (!in_array($temp, $temp_array)) {
$temp2_array[] = $temp;
}
Since you are adding instances in the array, make sure that the array in_array()
uses strict mode comparison:
$temp = new tempClass($x, $y);
if (!in_array($temp, $temp_array, true)) {
$temp2_array[] = $temp;
}
Also, you need to understand that 2 different instances of a class, even if they hold the same data, are still 2 different instances. You'll need to create your own loop and compare your instances manually if you which to know if 2 instances are the same.
You can omit strict mode which will compare the members of the class, but as soon as you have a different member, it will be non-equal.
$temp = new tempClass($x, $y);
if (!in_array($temp, $temp_array)) {
$temp2_array[] = $temp;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With