-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_with_commas.php
More file actions
59 lines (30 loc) · 1.16 KB
/
list_with_commas.php
File metadata and controls
59 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
$physicistsString = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark';
$physicistsArray = explode(', ', $physicistsString);
function humanizedList(array $ArrayIN, $isAsort = false) {
if ($isAsort) {
// resort by last name first
// foreach ($ArrayIN as $key => &$value) {
// $tempArr = explode(' ', $value);
// array_unshift($tempArr, array_pop($tempArr));
// $value = implode(" ", $tempArr);
// }
// sort($ArrayIN);
// foreach ($ArrayIN as $key => &$value) {
// $tempArr = explode(' ', $value);
// array_push($tempArr, array_shift($tempArr));
// $value = implode(" ", $tempArr);
// }
usort($ArrayIN, function($a, $b) {
$a = substr(strrchr($a, ' '), 1);
$b = substr(strrchr($b, ' '), 1);
return strcmp($a, $b);
});
}
array_push($ArrayIN, ("and " . array_pop($ArrayIN) . PHP_EOL));
$famousFakePhysicists = implode(", ", $ArrayIN);
return $famousFakePhysicists;
}
// Humanize that list
$famousFakePhysicists = humanizedList($physicistsArray, true);
echo "Some of the most famous fictional theoretical physicists are {$famousFakePhysicists}.";