1

I I have tried to remove null or empty values from array listing but not working.

Following is my array output..

Array
(
    [0] => Array
        (
            [post_id] => 1173
            [post_content] => Rocking Tips
            [comment_id] => 1173
            [comment_content] => Nice Post 
            [comment_date] => 
            [user_id] => 
            [username] => 
            [email] => 
            [first_name] => 
            [last_name] => 
        )

    [1] => Array
        (
            [post_id] => 
            [post_content] => 
            [comment_id] => 
            [comment_content] => 
            [comment_date] => 
            [user_id] => 
            [username] => 
            [email] => 
            [first_name] => 
            [last_name] => 
        )

    [2] => Array
        (
            [post_id] => 1173
            [post_content] => Rocking Tips
            [comment_id] => 1176
            [comment_content] => WOnder
            [comment_date] => 2020-05-21 21:10:04
            [user_id] => 168
            [username] => kkkk20
            [email] => [email protected]
            [first_name] => sam
            [last_name] => test

            [reply] => Array
                (
                    [comment_id] => 1177
                    [comment_details] => Thank YOu
                    [comment_date] => 2020-05-21 21:12:14
                    [user_id] => 179

                    [username] => test20201
                    [email] => [email protected]
                    [replay] => 
                )

        )

)

I have tried to remove empty array value using array_filter() but still empty value is showing when i print my array.

I also tried like,

$filtered = array_filter($myArray, function($var){return !is_null($var);} );
echo "<pre>List Data";print_r($filtered);

I want to remove 1 index array from list

1 Answer 1

2

An array whose values are empty strings is not the same as NULL. You need to test the values.

You can call array_filter() on the element. This will return all the non-empty values in the array; if all the values are empty it will return an empty array, which will be condidered falsey by the outer array_filter().

$filtered = array_filter($myArray, function($var){return array_filter($var);} );

Or you could just check whether a specific element such as post_id is empty:

$filtered = array_filter($myArray, function($var){return !empty($var['post_id']);} );
Sign up to request clarification or add additional context in comments.

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.