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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭