Flatten data in nested arrays

I was getting some data out of RSForms that wasn't structured in a way that was easy to manage, so I used the following routine to flatten all the data into a single array of variables and their values. It takes checkbox, radio button and list box selections and turns them into simpler variable.

function flatten_data($Data, $KeepKey='')
// takes whatever is passed to it an turns it into a flat array of parameters
{
    $Parameter = array();
    foreach($Data as $key=>$item)
    {
        $Type = gettype($item);
        if($Type == "array")
        {
            if(sizeof($item)==1) // if the array only has one item
            {
                $NewParameter = flatten_data($item, $key);
            }
            else $NewParameter = flatten_data($item);
        }
        else
        {
            if($KeepKey!='') $Parameter[$KeepKey]=$item;
            else $Parameter[$key]=$item;
        }
        if($NewParameter) $Parameter = array_merge($Parameter, $NewParameter);
    }
    return $Parameter;
}

You have no rights to post comments