0
Array (
      [0] => Angiogram - $10,000    
      [1] => 
      [2] => 
      [3] => 
      )

This is my array format and I need to remove all empty index from this array. I tried Using array_filter() but it is not working. Please help. I need result like:

Array([0]=> Angiogram-$10,000)
9
  • 4
    $array = array_filter($array); works perfectly well Commented Apr 6, 2015 at 11:22
  • array_filter is not working , Thanks For the reply Commented Apr 6, 2015 at 11:24
  • 2
    Then you don't have empty elements! What is the output of: var_dump($arr); ? Commented Apr 6, 2015 at 11:24
  • DEMO of array_filter() working for your array (as posted here) Commented Apr 6, 2015 at 11:25
  • 2
    string(65) "" suggests a lot of hidden characters such as nulls in a 65-byte string, clearly it isn't empty Commented Apr 6, 2015 at 11:33

3 Answers 3

2

Try this..

 $array=array("Angiogram - $10,000","","","","");
 $removeempty=array_filter($array);
 print_r($removeempty);

  or

$array = array_filter(array_map('trim', $array));
print_r($array);

    Ans:

    Array ( [0] => Angiogram - $10,000 )
Sign up to request clarification or add additional context in comments.

Comments

2

Please try like this,

array_filter(array_map('trim', $array))

1 Comment

Simplify even further to $myArray = array_filter($myArray, 'trim'); and eliminate the need to call array_map() completely
0

If array_filter($array) doesn't work it means that you array isn't empty!

Try this:

$array = array("1", "2", "3", "","5");


$clearArray = var_dump(removeEmpty($array));

function removeEmpty($array) {
  return array_filter($array, 'removeEmpty_internal');
}

function removeEmpty_internal($value) {
  return !empty($value) || $value === 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.