Skip to content

Instantly share code, notes, and snippets.

@allroundexperts
Created December 4, 2018 15:30
Show Gist options
  • Save allroundexperts/662da86e99cd6d7475115d522f5484a5 to your computer and use it in GitHub Desktop.
Save allroundexperts/662da86e99cd6d7475115d522f5484a5 to your computer and use it in GitHub Desktop.
Laravel PHP Developer Coding Challenge
<?php
require_once('./json.php'); //Importing the data file assuming it lies in the same directory as challenge.php file
$peopleArray = json_decode($people)->data;
//Making an array with all the emails and adding an extra property to $peopleArray
foreach($peopleArray as $key => $peopleObj){
$emails[] = $peopleObj->email;
$peopleObj->name = $peopleObj->first_name.' '.$peopleObj->last_name;
}
//Sorting $peopleArray by age in descending order.
usort($peopleArray, function($prev,$next){
if ($prev->age === $next->age) {
return 0;
}
return ($prev->age < $next->age) ? 1 : -1;
});
$emails_list = implode($emails,','); //Converting array to comma saperated elements.
$newData = json_encode(['data' => $peopleArray]); //Creating json using the mutated $peopleArray.
<?php
$people = '{"data":[{"first_name":"jake","last_name":"bennett","age":31,"email":"[email protected]","secret":"VXNlIHRoaXMgc2VjcmV0IHBocmFzZSBzb21ld2hlcmUgaW4geW91ciBjb2RlJ3MgY29tbWVudHM="},{"first_name":"jordon","last_name":"brill","age":85,"email": "[email protected]","secret":"YWxidXF1ZXJxdWUuIHNub3JrZWwu"}]}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment