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

递归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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?