I have two Arrays with Objects in it. I want to compare each property of the array with each other.
function compare ($array1, $array2)
{
$uniqueArray = array();
for ($i = 0; $i < count($array1); $i++)
{
for ($j = 0; $j < count($array2); $j++)
{
if(levenshtein($array1[$i]->getCompany(), $array2[$j]- >getCompany() > 0 || levenshtein($array1[$i]->getCompany(), $array2[$j]->getCompany()) < 3))
{
//add values to $unqiueArray
}
}
}
print_r($uniqueArray);
}
I'm not sure if my code is right. The iteration over the arrays and then the compare is that the right approach?
Object Properties:
private $_company;
private $_firstname;
private $_sirname;
private $_street;
private $_streetnumber;
private $_plz;
private $_place;
All prperties are strings.
You shouldn't use for (expr1; expr2; expr3) for iterating arrays; it's better to use foreach (array_expression as $value)
Also, you are comparing every element on array1 with every element on array2, but if there's a match you compare them again later.
Try something like this
foreach($array1 as $k1 => $v1) {
foreach($array2 as $k2 => $v2) {
if(your_condition()) {
$uniqueArray[] = $v1;
unset($array2[$k2])
}
}
}
Or maybe do some research on array_uintersect or array_walk
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