递归PHP函数,复制多维数组但替换空值

时间:2011-01-28 12:18:13

标签: php arrays multidimensional-array

我有一个可以是任何大小或深度的多维数组。我基本上尝试用值替换空值,但仅在某些情况下。这是一个数组的例子,它非常大但我想很好地说明我的观点:

[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] => 
                    )

            )

    )

我要做的是找到数组中的任何元素为空,然后用值替换空值。我有一系列异常,其中不替换空元素。这是我目前正在处理的功能:

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

有人可以帮助我调整我的功能或指向更好的方向吗?我尝试了array_walk_recursive,但它不能用于我的数组。

2 个答案:

答案 0 :(得分:2)

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

此处您不会将结果值返回到任何地方。可能这就是问题

答案 1 :(得分:2)

您需要指定递归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;
}