Skip to content

Instantly share code, notes, and snippets.

@lukebouch
Created May 6, 2022 21:41
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 lukebouch/9b4584f9fd4d16497492bbd9d6e8bd37 to your computer and use it in GitHub Desktop.
Save lukebouch/9b4584f9fd4d16497492bbd9d6e8bd37 to your computer and use it in GitHub Desktop.
// Super Secret Spy Award
$people = '{"data":[{"first_name":"jake","last_name":"bennett","age":31,"email":"jake@bennett.com","secret":"VXNlIHRoaXMgc2VjcmV0IHBocmFzZSBzb21ld2hlcmUgaW4geW91ciBjb2RlJ3MgY29tbWVudHM="},{"first_name":"jordon","last_name":"brill","age":85,"email": "jordon@brill.com","secret":"YWxidXF1ZXJxdWUuIHNub3JrZWwu"},]}';
$correctedJSON = preg_replace_callback("/(?:}(,)[^{])/", function($matches) {
return str_replace(",", "", $matches[0]);
}
, $people);
$data = collect(json_decode($correctedJSON)->data);
$emailAddresses = implode(', ', $data->map(function($person) {
return $person->email;
})->toArray());
$newData = $data->each(function($person) {
return $person->name = $person->first_name . ' ' . $person->last_name;
})->sortByDesc('age');
echo $emailAddresses, $newData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment