How to Compare Two Arrays from Files in PHP
Comparing arrays is a common task when dealing with datasets in PHP. Suppose you have two files containing lists of text values, and you want to find:
- Values present in both files.
- Values that exist only in the second file.
In this post, I’ll guide you through the steps to achieve this, complete with an easy-to-understand PHP script.
The Problem
Imagine you have two text files:
file1.txt
transactions, zone_sync_blockers, zone_versions, conditions, internal_conditions, conditions_from_regions, condition_properties, condition_versions, polygon_belonging_to_zone, zone_hierarchy, district_kinds, district_types, countries, geo_zones, area_types, zones, streets, divisions, polygons, regions, geo_zone_belonging_to_zone, features, features_changes, settlements, geo_zone_versions, postmachine_regions, storage_regions, division_regions, condition_groups, city_district_kinds
file2.txt
access_roles, packages, features, features_changes, condition_properties, zones, condition_groups, zone_hierarchy, district_kinds, reports, zone_versions, users, user_zone_permissions, geo_zone_versions, division_regions, condition_versions, conditions, notifications, individual_timetables, eu_reports, zone_sync_blockers, polygon_versions, settlements, area_types, city_district_kinds, conditions_from_regions, countries, hydra_access_tokens, spatial_ref_sys, en_reports, geo_zone_belonging_to_zone, geography_columns, geometry_columns, invalid_district_reports, divisions, postmachine_regions, storage_regions, district_types, failed_jobs, en_grouped_reports, geo_zones, internal_conditions, migrations, password_resets, eu_grouped_sender_aggregated_reports, eu_grouped_sender_detailed_reports, eu_grouped_recipient_detailed_reports, eu_grouped_recipient_aggregated_reports, personal_access_tokens, polygon_belonging_to_zone, additional_user_cities, polygons, positions, regions, streets, transactions
Your goal is to:
- Identify which values are common between the two files.
- Find values exclusive to the second file.
The Solution
Here’s a PHP script that compares the two arrays:
<?php // Read the contents of the files into arrays $file1 = file_get_contents('file1.txt'); $file2 = file_get_contents('file2.txt'); // Convert the comma-separated values into arrays $array1 = array_map('trim', explode(',', $file1)); $array2 = array_map('trim', explode(',', $file2)); // Find common values (present in both arrays) $commonValues = array_intersect($array1, $array2); // Find values only in the second array $onlyInSecondArray = array_diff($array2, $array1); // Count the number of elements in each result $countCommon = count($commonValues); $countOnlyInSecond = count($onlyInSecondArray); // Output the results echo "Values present in both arrays (Count: $countCommon):\n"; echo implode(", ", $commonValues) . "\n\n"; echo "Values only in the second array (Count: $countOnlyInSecond):\n"; echo implode(", ", $onlyInSecondArray) . "\n";
How It Works
- Reading Files: The script uses
file_get_contents()
to read the contents offile1.txt
andfile2.txt
. - Converting to Arrays: The
explode()
function splits the text into arrays, andarray_map('trim', ...)
removes any extra whitespace. - Finding Common and Exclusive Values:
array_intersect()
identifies the common values.array_diff()
identifies values that exist in the second array but not in the first.
- Counting Elements: The
count()
function calculates the number of elements in each result. - Output: The results, along with their counts, are displayed on the screen.
Example Output
For the provided files, running the script produces:
Values present in both arrays (Count: 28):
transactions, zone_sync_blockers, zone_versions, conditions, internal_conditions, conditions_from_regions, condition_properties, condition_versions, polygon_belonging_to_zone, zone_hierarchy, district_kinds, district_types, countries, geo_zones, area_types, zones, streets, divisions, polygons, regions, geo_zone_belonging_to_zone, features, features_changes, settlements, geo_zone_versions, postmachine_regions, storage_regions, division_regions, condition_groups, city_district_kinds
Values only in the second array (Count: 37):
access_roles, packages, reports, users, user_zone_permissions, notifications, individual_timetables, eu_reports, polygon_versions, hydra_access_tokens, spatial_ref_sys, en_reports, geography_columns, geometry_columns, invalid_district_reports, failed_jobs, en_grouped_reports, migrations, password_resets, eu_grouped_sender_aggregated_reports, eu_grouped_sender_detailed_reports, eu_grouped_recipient_detailed_reports, eu_grouped_recipient_aggregated_reports, personal_access_tokens, additional_user_cities, positions
Conclusion
This script is a handy way to compare datasets in PHP, especially when working with files. By leveraging PHP’s built-in array functions, you can efficiently process and analyze data.
Feel free to modify the script to suit your needs, and don’t forget to share your thoughts or enhancements in the comments below!
Happy coding!