I have a multidimensional array that could be any size or depth. I'm basically trying replace empty values with a value but only in certain cases. Here is an example of the array, it's quite large but I want to illustrate my point well:

[field_ter] => 
[field_title] => Array
    (
        [0] => Array
            (
                [value] => 
            )

    )

[field_firstnames] => Array
    (
        [0] => Array
            (
                [value] => test9
            )

    )

[field_birth] => Array
    (
        [0] => Array
            (
                [value] => 
            )

    )

[field_postal] => Array
    (
        [0] => Array
            (
                [value] => 
            )

    )

[group_certificates] => Array
    (
        [0] => Array
            (
                [_delta] => 0
                [field_cert_details] => Array
                    (
                        [value] => 
                    )

                [field_cert_issuedate] => Array
                    (
                        [value] => Array
                            (
                                [date] => 
                            )

                    )

                [field_cert_expiry] => Array
                    (
                        [value] => Array
                            (
                                [date] => 
                            )

                    )

                [field_cert_issue_country] => Array
                    (
                        [value] => 
                    )

                [field_cert_limits] => Array
                    (
                        [value] => 
                    )

            )

        [1] => Array
            (
                [_delta] => 1
                [field_cert_details] => Array
                    (
                        [value] => 
                    )

                [field_cert_issuedate] => Array
                    (
                        [value] => Array
                            (
                                [date] => 
                            )

                    )

                [field_cert_expiry] => Array
                    (
                        [value] => Array
                            (
                                [date] => 
                            )

                    )

                [field_cert_issue_country] => Array
                    (
                        [value] => 
                    )

                [field_cert_limits] => Array
                    (
                        [value] => 
                    )

            )

    )

What I'm trying to do is find any element in the array that is empty, then replace the empty value with a value. I have an array of exceptions where the empty element is not replaced. This is the function I'm working on at the moment:

function check_empty(&$array) { 
    $exceptions = array('changed', 'form_build_id','date', 'status', 'op');
    // This is the array we will return at the end of the function
    $new_array = array();
    foreach($array as $key => $value) {
        if(is_array($value)) {
            check_empty($value);
        }
        elseif ($value == '' && !in_array($key, $exceptions)) {
            $new_array[$key] = '$$$';
        }
        else {
            $new_array[$key] = $value;
        }
    }
    return $new_array;
}

Could someone help me tweak my function or point me in the direction of a better way? I tried array_walk_recursive but it doesn't work with my arrays.

Comments

Please refer to stackoverflow.com/editing-help for help on how to format your question. Here, we use something called Markdown in lieu of usual HTML. There are some toolbar icons just above the textarea that can help you with the formatting. Welcome to the site!

Written by BoltClock

Accepted Answer

You need to assign the return value of the recursive check_empty:

function check_empty($array) { 
    $exceptions = array('changed', 'form_build_id','date', 'status', 'op');
    // This is the array we will return at the end of the function
    $new_array = array();

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $new_array[$key] = check_empty($value);
        }
        elseif ($value == '' && !in_array($key, $exceptions)) {
            $new_array[$key] = '$$$';
        }
        else {
            $new_array[$key] = $value;
        }
    }
    return $new_array;
}
Written by NikiC
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki