0

I'm trying to compare two arrays.

I have a fixed array in my code. If the array (extracted from a csv) does not match the fixed array, the values that are deviating from the fixed array should be returned? How can I achieve this?

What I have tried;

$columnsFromFile = array_filter(array_map('trim', array_map('strtolower', $line)));
$differences = array_diff_key($columnsFromFile, CsvFile::CSV_COLUMNS);

I've also tried something like this;

$differences = array_merge(CsvFile::CSV_COLUMNS, array_diff_key($columnsFromFile, CsvFile::CSV_COLUMNS));

But the actual matches is returned.

Mentos93
  • 571
  • 1
  • 7
  • 26

1 Answers1

0

First, check for identical match, if not, then return only the new data.

// if associative arrays...
if($new_array!=$fixed_array){
    $differences=array_diff_assoc($new_array,$fixed_array); // this will return only the new/changed keys/values.
}else{
    // identical / no change
}

If you are not working with associative arrays, reference array_equal() function @ https://stackoverflow.com/a/6922213/2943403

Community
  • 1
  • 1
mickmackusa
  • 37,596
  • 11
  • 75
  • 105
  • @Mentos93 Let's move your question toward resolution. If my answer solves your issue, please mark it with the green tick. If not, please leave feedback as a comment which explains what isn't quite right for your case. – mickmackusa Apr 13 '17 at 00:06
  • @Mentos93 What is the status of this question? If my answer solves your issue please grant it the green tick to mark your question resolved. If not, please leave me an informative comment so that I can help. – mickmackusa Apr 20 '17 at 07:59