fork download
  1. <pre>
  2. <?php
  3. $array = array('','key'=>'','Name'=>'A','id'=>3, 'foo' => '', 'hello!', 'anotherKey' => '');
  4.  
  5. $firstNonEmpty = 0;
  6. $i = 0;
  7. foreach ($array as $val) {
  8. if ($val != '') {
  9. $firstNonEmpty = $i;
  10. break;
  11. }
  12. ++$i;
  13. }
  14.  
  15. $lastNonEmpty = $count = count($array);
  16. end($array);
  17. for ($i = $count; $i > 0; --$i) {
  18. if (current($array) != '') {
  19. $lastNonEmpty = $i;
  20. break;
  21. }
  22. prev($array);
  23. }
  24.  
  25. $array = array_slice($array, $firstNonEmpty, $lastNonEmpty - $firstNonEmpty);
  26.  
  27. print_r($array);
  28.  
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
<pre>
Array
(
    [Name] => A
    [id] => 3
    [foo] => 
    [0] => hello!
)