By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Remove Negatives and Reverse - PHP Array Task

+4 votes
  • Read an array of integers
  • Remove all negative numbers from it and  print the remaining elements in reversed order
  • In case of no elements left in the array, print "empty"

remove negatives and reverse

asked in PHP category by user mitko

1 Answer

+3 votes

My solution:


$arr = array_map(function ($element) {
    return ($element > 0) ? $element : false;
}, explode(" ", readline()));

$arr = array_filter($arr);
$arr = array_reverse($arr);

if (count($arr) > 0) {
    echo implode(" ", $arr);
} else {
    echo "empty";
answered by user icabe