1

i have an array $mainArray of arrays and i would like to remove / undset the arrays that vave keys with no value.

here is my array:

Array
(
[0] => Array
    (
        [msg_id] => 203
        [comment] => Array
            (
                [0] => Array
                    (
                        [com_id] => 
                    )
            )
    )
[1] => Array
    (
        [msg_id] => 202
        [comment] => Array
            (
                [0] => Array
                    (
                        [com_id] => 196
                    )
                [1] => Array
                    (
                        [com_id] => 197
                    )
                [2] => Array
                    (
                        [com_id] => 
                    )
            )
    )
[2] => Array
    (
        [msg_id] => 201
        [comment] => Array
            (
                [0] => Array
                    (
                        [com_id] => 198
                    )
                [1] => Array
                    (
                        [com_id] => 
                    )
            )
    )
)

In this case i would like to look inside comment array arrays and see if there are any of them that have empty values. The best case scenario would be to remove the comment array entirely if all sub arrays are empty.

nut im of with leaving the comment hey there just null

this array should become:

Array
(
    [0] => Array
        (
            [msg_id] => 203
        )
    [1] => Array
        (
            [msg_id] => 202
            [comment] => Array
                (
                    [0] => Array
                        (
                            [com_id] => 196
                        )
                    [1] => Array
                        (
                            [com_id] => 197
                        )
                )
        )
    [2] => Array
        (
            [msg_id] => 201
        )
)

any ideas on how to proceed?

thanks.

3 Answers 3

2

array_filter() is what you're after. Particularly a recursive version. The following was taken from a comment on the PHP Doc:.

function array_filter_recursive($array, $callback = null) {
    foreach ($array as $key => & $value) {
        if (is_array($value)) {
            $value = array_filter_recursive($value, $callback);
        }
        else {
            if ( ! is_null($callback)) {
                if ( ! $callback($value)) {
                    unset($array[$key]);
                }
            }
            else {
                if ( ! (bool) $value) {
                    unset($array[$key]);
                }
            }
        }
    }
    unset($value);

    return $array;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use php's unset() to unset any array key/value.

More info on this link https://www.php.net/unset

in your case the code can be, (i have not tested it. but let me know if you have any problem and i can fix it)

function unsetCommentFromArray($mainArray) {
    foreach($mainArray as $key => $value) {
        foreach($value['comment'] as $k => $v) {
            if(empty($v['com_id'])) {
                unset($mainArray[$key]['comment'][$k]);
            }
        }   
    }
    return $mainArray;
}

Comments

1
$array = array_map(function ($i) {
    $i['comment'] = array_filter($i['comment'], function ($c) { return $c['com_id']; });
    return array_filter($i);
}, $array);

Requires PHP 5.3 or higher.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.