<?php $famousFakePhysicists = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark'; $physicistsArray = explode(', ', $famousFakePhysicists); function humanizedList($physicistsArray, $sort = false) { if ($sort) { sort($physicistsArray); } $name = 'and ' . array_pop($physicistsArray); array_push($physicistsArray, $name); $famousFakePhysicists = implode(', ', $physicistsArray); return $famousFakePhysicists; } $famousFakePhysicists = humanizedList($physicistsArray); echo "Some of the most famous fictional theoretical physicists are {$famousFakePhysicists}.";
<?php // Step 3: Update your code to list the physicists by first name, in alphabetical order. // Step 4: Create a second argument to make alphabetical sorting optional. // Step 5: Default alphabetical sorting to false. If no args are passed, no sorting takes place. // Step 1 & 2: Converts array into list n1, n2, ..., and n3 function humanizedList($array, $sort = false) { if ($sort) { sort($array); } $endList = ', and ' . array_pop($array); // var_dump($endList); $humanizedList = implode(', ', $array) . $endList; // var_dump($humanizedList); return $humanizedList; } // List of famous peeps $physicistsString = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark'; // TODO: Convert the string into an array $physicistsArray = []; $physicistsArray = explode(', ', $physicistsString); // var_dump($physicistsArray); // Humanize that list $famousFakePhysicists = humanizedList($physicistsArray); $sortedFakeGuys = humanizedList($physicistsArray, 3); // Output sentence echo "Some of the most famous fictional theoretical physicists are {$famousFakePhysicists}." . PHP_EOL . PHP_EOL; echo "Some of the most famous fictional theoretical physicists are {$sortedFakeGuys}." . PHP_EOL;
<?php $famousFakePhysicistsArray = ["Gordon Freeman", "Samantha Carter", "Sheldon Cooper", "Quinn Mallory", "Bruce Banner", "Tony Stark"]; function humanizedList($array, $sort = false) { if ($sort) { sort($array); } $last = array_pop($array); $famousFakePhysicists = implode(", ", $array); echo "Some of the most famous fictional theoretical physicists are {$famousFakePhysicists} and {$last}."; } humanizedList($famousFakePhysicistsArray, true);
<?php // List of famous peeps $physicistsString = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark'; //removes the space and commas turns into an array $newPhysicistsArray = explode(', ', $physicistsString); //creates the alphabetized version of statement echoed below... $newPhysicistsString2 = humanizedList($newPhysicistsArray, true); //function to alphabetize list and concatenate new string function humanizedList($array, $sorting = false) { if ($sorting) { //if true, sorts alphabetically sort($array); } $lastNamePop = array_pop($array); //pops last name off array (Tony Stark) $physicistsString2 = implode(', ', $array) . ', and ' . $lastNamePop; // adds back last name popped off the list and concatenates return $physicistsString2; } echo "Some of the most famous fictional theoretical physicists are {$newPhysicistsString2}." . PHP_EOL;
<?php $physicistsString = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark'; $physicistsArray = explode(', ', $physicistsString); // print_r($famousFakePhysicists); // $removed = array_pop($physicistsArray); // array_push($physicistsArray, 'and', $removed); // $new_text = 'Some of the most famous fictional theoretical physicists are '; // array_unshift($physicistsArray, $new_text); // $new_string = implode(', ', $physicistsArray); // echo"$new_string\n"; // function humanizedList($array) { // sort($array); // $removed = array_pop($array); // $new_text = 'Some of the most famous fictional theoretical physicists are '; // $new_string = $new_text . implode(', ', $array) . ', and' . ' ' . $removed ; // return "$new_string\n"; // } // echo humanizedList($physicistsArray); function humanizedList($array, $to_sort = false) { if ($to_sort) { sort($array); } $removed = array_pop($array); $new_text = 'Some of the most famous fictional theoretical physicists are '; $new_string = $new_text . implode(', ', $array) . ', and' . ' ' . $removed; return "{$new_string}\n"; } echo humanizedList($physicistsArray, true);
<?php // Converts array into list n1, n2, ..., and n3 function humanizedList($array, $sort = false) { // TODO: Convert the string into an array if ($sort) { sort($array); } $popped_a = array_pop($array); // [] means push onto array like array_push($array, $popped_a); $array[] = 'and ' . $popped_a; // Humanize that list $physicistsString = implode(', ', $array); return $physicistsString; } $physicistsString = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark'; $physicistsArray = explode(', ', $physicistsString); $famousFakephysicists = humanizedList($physicistsArray, true); // Output sentence echo "Some of the most famous fictional theoretical physicists are {$famousFakephysicists}.\n";