Skip to content

Instantly share code, notes, and snippets.

@nullthoughts
Created July 12, 2018 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullthoughts/898e8f9b65954f41097b52af430a5cbd to your computer and use it in GitHub Desktop.
Save nullthoughts/898e8f9b65954f41097b52af430a5cbd to your computer and use it in GitHub Desktop.
Laravel helper for merging two Json files matched by key value
public static function mergeJson($to, $from, $matchBy, $key)
{
$to = json_decode(file_get_contents(storage_path($to)));
$from = json_decode(file_get_contents(storage_path($from)));
$values = [];
foreach($from as $object) {
$values[$object->{$matchBy}] = $object->{$key};
}
$output = collect();
foreach($to as $object) {
$object->{$key} = new \stdClass;
$object->{$key} = $values[$object->{$matchBy}];
$output = $output->push($object);
}
return $output->toJson();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment