Created
July 12, 2018 19:54
-
-
Save nullthoughts/898e8f9b65954f41097b52af430a5cbd to your computer and use it in GitHub Desktop.
Laravel helper for merging two Json files matched by key value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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