Skip to content

Instantly share code, notes, and snippets.

View sakibbuddy's full-sized avatar

Anisur Rahaman Sakib sakibbuddy

View GitHub Profile
@cjthompson
cjthompson / arrayDifference.php
Last active April 7, 2024 00:30
PHP: Compare two arrays and find the differences between them. Supports items that are arrays by serializing them for comparison. Returns an array of insertions (only in the second array) and deletions (only in the first array).
<?php
/**
* Compare two arrays and return a list of items only in array1 (deletions) and only in array2 (insertions)
*
* @param array $array1 The 'original' array, for comparison. Items that exist here only are considered to be deleted (deletions).
* @param array $array2 The 'new' array. Items that exist here only are considered to be new items (insertions).
* @param array $keysToCompare A list of array key names that should be used for comparison of arrays (ignore all other keys)
* @return array[] array with keys 'insertions' and 'deletions'
*/
public static function arrayDifference(array $array1, array $array2, array $keysToCompare = null) {