drwdvftp423507 2015-12-04 20:42
浏览 43
已采纳

如何将PHP $ _POST数组作为可用作名称属性的字符串返回?

Question:

I have an array of post values that looks like:

$_POST['children'] = array(
[0]=>array(
'fname' => 'name',
'mname' => 'mname',
'lname' => 'lname,
'dob' => '10/17/1992
),
[1]=>array(
'fname' => 'name',
'mname' => 'mname',
'lname' => 'lname,
'dob' => '10/17/1992
),
[2]=>array(
'fname' => 'name',
'mname' => 'mname',
'lname' => 'lname,
'dob' => '10/17/1992
)
);
// and so on

I have a script set up in my my view functions that checks for old input, and repopulates the values in the case that the form does not validate. I need to find a way to return the above array as a series of key/value pairs i.e.

'children[0][fname]' = 'name'
'children[0][mname]' = 'mname'
'children[0][lname]' = 'lname'
// ... and so on for all fields

Ideally, I would like this to work with an array of any depth, which makes me think I need some sort of recursive function to format the keys. I am having a terrible time getting my head around how to do this.

What I have tried

I have been working with the following function:

function flatten($post_data, $prefix = '') {
    $result = array();
    foreach($post_data as $key => $value) {
        if(is_array($value)) {
            if($prefix == ''){
                $result = $result + flatten($value, $prefix. $key );    
            }else{
                $result = $result + flatten($value, $prefix. '[' . $key . ']'); 
            }

        }
        else {
            $result[$prefix . $key .''] = $value;
        }

    }
    return $result;
}

This gets me somewhat close, but isn't quite right. It returns the following when I feed it my $_POST array

[children[1]fname] => test
[children[1]mname] => test
[children[1]lname] => test
[children[1]dob] => 
// Expecting: children[1][fname] => test
// ...

Or is there potentially an easier way to accomplish this?

  • 写回答

1条回答 默认 最新

  • douao7937 2015-12-04 21:05
    关注

    What ended up working for me:

    function flatten($post_data, $prefix = '') {
        $result = array();
        foreach($post_data as $key => $value) {
            if(is_array($value)) {
                if($prefix == ''){
                    $result = $result + flatten($value, $prefix. $key );    
                }else{
                    $result = $result + flatten($value, $prefix. '[' . $key . ']'); 
                }
    
            }
            else {
                if( $prefix == ''){
                    $result[$prefix . $key.''] = $value;
                }else{
                    $result[$prefix . '['.$key.']'.''] = $value;
                } 
    
            }
    
        }
        return $result;
    }
    

    it wasn't accounting for the return value of the last call of the recursive function being a scalar value. The addition of these this if/else statement seems to have resolved it.

    if( $prefix == ''){
        $result[$prefix . $key.''] = $value;
    }else{
        $result[$prefix . '['.$key.']'.''] = $value;
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀