PHP code example of xpaw / compare-arrays

1. Go to this page and download the library: Download xpaw/compare-arrays library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xpaw / compare-arrays example snippets


use xPaw\CompareArrays\CompareArrays;

$oldArray = [
	'user' => [
		'name' => 'John',
		'age' => 30,
		'settings' => [
			'darkMode' => true,
			'notifications' => true
		]
	]
];

$newArray = [
	'user' => [
		'name' => 'John Doe',
		'age' => 30,
		'settings' => [
			'darkMode' => true,
			'notifications' => false
		],
		'lastLogin' => '2025-03-20'
	]
];

$differences = CompareArrays::Diff($oldArray, $newArray);
print_r($differences);

$flattened = CompareArrays::Flatten($differences);
print_r($flattened);

// Using a dot as separator
$flattened = CompareArrays::Flatten($differences, '.');

$differences = CompareArrays::Diff(
	['value' => 0.1],
	['value' => 0.1 + 0.00000001]
);
// Result: empty array (no differences)

$differences = CompareArrays::Diff(
	['value' => 0.1],
	['value' => 0.1 + 0.0001]
);
// Result: detects difference