开发者

Recursive PHP function that copies multidimensional array but replaces empty values

开发者 https://www.devze.com 2023-02-07 11:25 出处:网络
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 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
                            (
     开发者_JS百科                           [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.


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;
}


if(is_array($value)) {
  check_empty($value);
}

Here you don't return resulting value to anywhere. Probably that's the problem

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号