dongxu3029
2011-01-28 12:18
浏览 117
已采纳

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

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.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtihe8614 2011-01-28 12:29
    已采纳

    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;
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongren4758 2011-01-28 12:28
    if(is_array($value)) {
      check_empty($value);
    }
    

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题