1
\$\begingroup\$

I have two arrays:

  • $row
  • $rowTwo

The only difference between these two arrays are the first two keys have slightly different names due problems out of my control.

I'm wondering if there is a cleaner way to do it. Or would it be simply easier to find a way to rename the arrays to match?

        $needUpdate = false;

        if (strcasecmp( $row['Trelease'] , $rowTwo['release'] ) != 0) {
            $needUpdate = true;
        }  else if (strcasecmp( $row['dateTime'] , $rowTwo['date'] ) != 0) {
            $needUpdate = true;
        } else if (strcasecmp( $row['defectID'] , $rowTwo['defectID'] ) != 0) {
            $needUpdate = true;
        } else if (strcasecmp( $row['type'] , $rowTwo['type'] ) != 0) {
            $needUpdate = true;
        } else if (strcasecmp( $row['severity'] , $rowTwo['severity'] ) != 0) {
            $needUpdate = true;
        } else if (strcasecmp( $row['summary'] , $rowTwo['summary'] ) != 0) {
            $needUpdate = true;
        }  else if (strcasecmp( $row['status'] , $rowTwo['status'] ) != 0) {
            $needUpdate = true;
        }  else if (strcasecmp( $row['link'] , $rowTwo['link'] ) != 0) {
            $needUpdate = true;
        }
\$\endgroup\$
2
  • \$\begingroup\$ Can you add elements of both arrays in the question \$\endgroup\$
    – Tushar
    Commented Aug 7, 2015 at 11:09
  • \$\begingroup\$ If you get the keys to match: php.net/array_diff \$\endgroup\$
    – Martijn
    Commented Aug 7, 2015 at 12:49

2 Answers 2

3
\$\begingroup\$

http://php.net/array_diff

Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

// Set the keys matching to the $rowTwo array
$row['release'] = $row['Trelease'];
$row['date'] = $row['dateTime'];

// Unset the original values as we no longer need these
unset($row['Trelease'], $row['dateTime']);

// Compare them:
$difference = array_diff($row, $rowTwo);
$needUpdate = count($difference)!==0;
\$\endgroup\$
1
\$\begingroup\$

The first thing I would do is introduce an equals function, because it's nicer to read than strcasecmp(...) != 0.

Secondly, all your if statements do exactly the same thing ($needUpdate = true;), so you can just combine them:

if (equals($row['Trelease'] , $rowTwo['release']) || equals($row['dateTime'] , $rowTwo['date'] || ...)) {
    $needUpdate = true;
}

function equals($a, $b) {
    return strcasecmp($a, $b) != 0;
}

You could also do it in a different way (the variable and function names can be improved):

function compareArrays($rowOne, $rowTwo) {
    $compare = array("Trelease" => "release", 
                    "dateTime" => "date", 
                    "defectID" => "defectID", 
                    ...]);
    foreach ($compare as $columnsRowOne => $columnsRowTwo) {
        if (equals($rowOne[$columnsRowOne], $rowTwo[$columnsRowTwo])) {
            return true;
        }
    }
    return false;
}

Initially, the code is a bit more complex, but it's more reusable and adding new items is easier, if that's something you expect in the future.

If you know that the only difference in the keys is always going to be in the first two keys, you could also handle that as a special case, remove it from the array, and use array_diff_assoc for the rest.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.