Skip to content

Instantly share code, notes, and snippets.

@karakhanyans
Created December 4, 2018 16:10
Show Gist options
  • Save karakhanyans/af1668a040d8568f39ac51b398860289 to your computer and use it in GitHub Desktop.
Save karakhanyans/af1668a040d8568f39ac51b398860289 to your computer and use it in GitHub Desktop.
<?php
//Sorting array by age
function ageSort($a, $b)
{
return ($a->age >= $b->age) ? -1 : 1;
}
$data = '{"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"}]}';
//Format data to array
$formattedData = json_decode($data)->data;
//Sort array by age
usort($formattedData, 'ageSort');
$emails = [];
foreach ($formattedData as $key => $value) {
//Create name column
$formattedData[$key]->name = $value->first_name . ' ' . $value->last_name;
//Push email to array
$emails[] = $value->email;
}
//Implode emails by comma
$emails = implode(',', $emails);
print_r($formattedData);
echo '<br/>';
print_r($emails);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment