PHP convert nested array to single array while concatenating keys?

前端 未结 7 1005
我在风中等你
我在风中等你 2020-12-10 04:24

Here is an example array:

 $foo = array(
           \'employer\' => array(
                    \'name\' => \'Foobar Inc\',
                    \'phone\         


        
7条回答
  •  情书的邮戳
    2020-12-10 05:07

    Something like this:

    function makeNonNestedRecursive(array &$out, $key, array $in){
        foreach($in as $k=>$v){
            if(is_array($v)){
                makeNonNestedRecursive($out, $key . $k . '_', $v);
            }else{
                $out[$key . $k] = $v;
            }
        }
    }
    
    function makeNonNested(array $in){
        $out = array();
        makeNonNestedRecursive($out, '', $in);
        return $out;
    }
    
    // Example
    $fooCompressed = makeNonNested($foo);
    

提交回复
热议问题