I've got two mySQL queries which pull data from my database. Without going into too much detail, after running the queries the results of both should be related when assigning to an array (i.e. array1[x] and array2[x] should be related by the x offset by virtue of the nature of the sql queries).
However this isn't robust enough if something happens where one of the queries returns less or more than the other, then the offset will be off making my for loop comparison (at the end of the code snippet below) faulty.
['companyid'] in both arrays is fixed and will not change, so is there a way of making that the index of the arrays and looping through it for comparison instead? Note that ['companyid'] can be any non-sequential number.
*SQL Query*
while ($row = $result->fetch()) {
$pastcompanies[] = array(
'companyid' => $row['id'],
'name' => $row['name'],
'contactid' => $row['contactid'],
'firstname' => $row['firstname'],
'lastname' => $row['lastname'],
'email' => $row['email']
);
}
*SQL Query*
while ($row = $result->fetch()) {
$nowcompanies[] = array(
'companyid' => $row['id'],
'name' => $row['name'],
'contactid' => $row['contactid'],
'firstname' => $row['firstname'],
'lastname' => $row['lastname'],
'email' => $row['email']
);
}
for ($x=0;$x<count($pastcompanies);$x++){
if ($pastcompanies[$x]['contactid']!=$nowcompanies[$x]['contactid']){
echo $pastcompanies[$x]['firstname']." has been replaced by "
.$nowcompanies[$x]['firstname'];
}
}