Thread: Array Question
View Single Post
10-18-2012, 03:35 PM
#3
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old

Ah thanks. I studied your code and came up with something very similar:

PHP Code:
    // format $_POST into a multi-dimensional array
    
foreach($_POST as $key => $value) {
        
preg_match('/^([0-9]+)-([A-z]+)-([0-9]+)$/'$key$matches);

        
// build new parent key
        
$parent_key $matches[1] . '-'.$matches[3];

        
// build new key
        
$newkey $matches[2];

        
// if empty value delete key else recreate
        
if(!$value)
            unset(
$_POST[$key]);
        else
            
$sorted[$parent_key][$newkey] = $value;

    }

    
// reformat some keys and values
    
foreach ($sorted as $array_key => $array) {

        
$hour     NULL;
        
$minute NULL;
        
$second NULL;

        foreach (
$array as $key => $value) {

            if ((
$key == "hr") & ($value 0)) {
                
$hour $value;
                unset(
$sorted[$array_key][$key]);
            }
            if ((
$key == "min") & ($value 0)) {
                
$minute $value;
                unset(
$sorted[$array_key][$key]);
            }
            elseif ((
$key == "sec") & ($value 0)) {
                
$second $value;
                unset(
$sorted[$array_key][$key]);
            }

        }

        
// convert hr:min:sec to seconds
        
if (isset($hour) && isset($minute) && isset($second)) {

            
$formatted $convert->time($hour"hour""second") + $convert->time($minute"minute""second") + $second;
            
$sorted[$array_key]['seconds'] = $formatted;

        }

    } 

Reply With Quote