[Solved] Remove Negatives and Reverse - PHP Array Task

  

3
Topic starter
  • 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

1 Answer
2

My solution:

<?php
 
$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";
}
Share: