Skip to content

Instantly share code, notes, and snippets.

@maximevalette
Created May 18, 2013 12:28
Show Gist options
  • Save maximevalette/5604240 to your computer and use it in GitHub Desktop.
Save maximevalette/5604240 to your computer and use it in GitHub Desktop.
Merges JSON files
<?php
$source = json_decode(file_get_contents('french.json'), true);
$dest = json_decode(file_get_contents('auto_french.json'), true);
function mergeArray($source, $dest) {
foreach ($dest as $k => $v) {
if (!array_key_exists($k, $source)) {
$source[$k] = $v;
} elseif (!is_string($v)) {
$source[$k] = mergeArray($source[$k], $v);
}
}
return $source;
}
$new = mergeArray($source, $dest);
file_put_contents('new_french.json', json_encode($new, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment