0

i have 1 array with more arrays inside, and im trying to remove one of those arrays if they have a null key or its empty. Im doing this on PHP.

What i have:

Array
(
    [0] => Array
        (
            [emp] => 
            [fun] => 1
            [data] => 
            [data_d] => 
            [desc] => 
        )

    [1] => Array
        (
            [emp] => teste
            [func] => 1
            [data] => 2021-08-30
            [data_d] => 2021-09-01
            [desc] => testetetetete
        )

    [2] => Array
        (
            [emp] => ersfewqef
            [func] => 2
            [data] => 2021-08-26
            [data_d] => 2021-08-28
            [desc] => dvgdgvqevgqe
        )
)

This means that the [0] will be removed because has one or more empty values. Is this possible? I have no clue on how to do this.

3
  • To clarify, are you looking at the values only? Your question states "a null key". Also, are you looking for null values only, or empty strings also? And what about numeric values of zero? Commented Aug 30, 2021 at 14:07
  • @AndrewHardiman empty keys or empty strings. Commented Aug 30, 2021 at 14:09
  • Empty strings AND null values? An empty string is not null. Commented Aug 30, 2021 at 14:10

4 Answers 4

1

You can use array_filter for this (maybe not the cleanest)

$result = array_filter($data, function($v, $k) {
    #return true of false :: if true, then item is added in $result
    return empty($v['emp']) || empty($v['data']) ... ;
}, ARRAY_FILTER_USE_BOTH );
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_filter to filter elements of an array using a callback function.

If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case.

PHP reference

2 Comments

yes but if it has one valid string the [0] wont be removed
your goal is to remove only the element or the whole array?
1

I think the other answers are on the right track with array_filter, but as far as how to identify which inner arrays have falsey values, you can just use array_filter again.

$result = array_filter($outer, fn($inner) => array_filter($inner) == $inner);

If an array is equal to its filtered value, then it does not contain any empty values, so using that comparison in your filter callback will remove all of the inner arrays with any empty values.

Just remember if you use this that any of the values listed here evaluate to false, so if you don't consider those "empty", you'll need to add a callback to the inner array_filter to make it more selective.

1 Comment

yes the others are on the right track but they wont remove the entire 'sub-array' it will only remove empty keys, strings. ill give a try to this, thanks
1

You can use array_filter and array_intersect to check for a number of needles in your haystack.

$newArray = array_filter($array, function($v) {
    if( empty(array_intersect($v, [null, ""])) && 
    empty(array_intersect(array_keys($v), [null, ""])) ) {
        return $v;
    }
});

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.