1

I have array,where is a lot of empty field. How can I remove the filled fields,and make new array with the empty fields. So,example:

[0] => Array
        (
            [id] => 26
            [user_type] => 2
            [user_name] => Julian
            [password] => b941da1629f4742de62796d51730edbb
            [fpassword] => 1
            [email] => [email protected]
            [name] => 
            [surname] => 
            [birthday] => 0000-00-00
            [country] => 
            [city] => 
            [adress] => 
            [post_code] => 
            [mob_number] => 77077412
            [tel_number] => 0
            [web_page] => 
            [registration_date] => 2016-11-19 05:03:05
            [active] => 1
            [activation_code] => 714779
            [last_login] => 2016-11-20 12:06:36
        )

So I want this:

[0] => Array
        (
            [name] => 
            [surname] => 
            [birthday] => 0000-00-00
            [country] => 
            [city] => 
            [adress] => 
            [post_code] => 
            [tel_number] => 0
            [web_page] => 

        )

I tired array_diff($data, array(''));,but nothing happend.Thx

2 Answers 2

3

You need a "reverse" array_filter, like:

$empty = array_filter($data, function($item) {
    return empty($item);
});

But it needs to be tuned for various values, for example date 0000-00-00 is not "falsy" so it won't be caught by empty. The rule is that you need to return true from array_filter's callable for any value that you consider "empty".

The code you need in this example will be:

$data = [
    'int' => 100,
    'str' => 'val',
    'empty' => '',
    'null' => null,
    'date' => '0000-00-00',
];
$empty = array_filter($data, function($item) {
    return empty($item) || '0000-00-00' === $item;
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this, in $a1 - just keep rules which values you want to see in new array.

$a1 = array('empty' => '', 'another_rule' => '0000-00-00');
$a2 = array(
            'id' => 26,
            'user_type' => 2,
            'name' => '',
            'birthday' => '0000-00-00',
        );
$result = array_intersect($a2, $a1);

1 Comment

Working! Thx u so much!

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.