Created
December 4, 2018 16:10
-
-
Save karakhanyans/af1668a040d8568f39ac51b398860289 to your computer and use it in GitHub Desktop.
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
<?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":"[email protected]","secret":"VXNlIHRoaXMgc2VjcmV0IHBocmFzZSBzb21ld2hlcmUgaW4geW91ciBjb2RlJ3MgY29tbWVudHM="},{"first_name":"jordon","last_name":"brill","age":85,"email": "[email protected]","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