0

i need a function to delete an array which include an empty element from multidimensional array in php suppose following is my array here i need to find out and delete array[1] and array[2] since element empty has no value.

$array[] = array(
   'name'=>'name1',
   'email'=>'email1',
   'empty'=>'NOT_EMPTY'
);
$array[] = array(
   'name'=>'name2',
   'email'=>'email2',
   'empty'=>''
);
$array[] = array(
   'name'=>'',
   'email'=>'',
   'empty'=>''
);

when i do

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

print_r($array);

i got the result

Array
(
    [0] => Array
        (
            [name] => name1
            [email] => email1
            [empty]=> NOT_EMPTY
        )

    [1] => Array
        (
            [name] => name2
            [email] => email2
        )

    [2] => Array
        (
        )

) 

BUT EXPECTED RESULT

Array
(

    [0] => Array
        (
            [name] => name2
            [email] => email2
            [empty]=> NOT_EMPTY
        )

)
2
  • We'll also need to see your array_filter() function code to understand how you're getting the results. Commented Oct 11, 2013 at 2:28
  • it is an inbuilt function
    – nikki
    Commented Oct 11, 2013 at 2:31

1 Answer 1

1

array_filter() on its own only unsets values that equate to false, not the entire array. you will need to loop, and if any array has missing element, then unset array, like:

foreach($array as $key => $a){
  if(count(array_filter($a)) < count($a)){
    unset($array[$key]);
  }
}

there probably is a better way, i'm just simple

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.