Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 81,169
Total Posts: 566,838
There are 44 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Array Question

Thread title: Array Question
Reply    
    Thread tools Search this thread Display Modes  
10-17-2012, 05:04 PM
#1
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  Array Question

PHP Code:
Array
(
    [
322-reps-1] => asdsd
    
[529-kg-1] => sdad
    
[529-reps-1] => asd
    
[529-kg-2] => 
    [
529-reps-2] => 
    [
35-hr-1] => asdasd
    
[35-min-1] => asd
    
[35-sec-1] => asd
    
[35-hr-2] => 
    [
35-min-2] => 
    [
35-sec-2] => 
)

$unsorted $_POST;

$sorted = array();
foreach(
$_POST as $key=>$value) {
    
preg_match('/^([0-9]+)-([A-z]+)-([0-9]+)$/'$key$matches);
    
$parent_key $matches[1] . '-'.$matches[3];

    
$sorted[$parent_key][$key] = $value;

Turns into:

PHP Code:
Array
(
    [
322-1] => Array
        (
            [
322-reps-1] => asdsd
        
)

    [
529-1] => Array
        (
            [
529-kg-1] => sdad
            
[529-reps-1] => asd
        
)

    [
529-2] => Array
        (
            [
529-kg-2] => 
            [
529-reps-2] => 
        )

    [
35-1] => Array
        (
            [
35-hr-1] => asdasd
            
[35-min-1] => asd
            
[35-sec-1] => asd
        
)

    [
35-2] => Array
        (
            [
35-hr-2] => 
            [
35-min-2] => 
            [
35-sec-2] => 
        )



How can I remove an array if ALL it's values are NULL?
How can I merge the hr, min and sec keys into time => hr:min:sec?

Any idea?

Reply With Quote
10-18-2012, 02:52 AM
#2
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,892
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old


How can I remove an array if ALL it's values are NULL?
Try something like

PHP Code:
<?php 
foreach($sorted as $a => $aKey){
    
$isNotNull=false;
    
    
//loop though each sub-array
    
foreach($a as $b){
        
// If value is not null, mark it so this part won't get deleted
        // You can save a few clock cycles by adding break here but don't,
        // the gains are minimal and break is always a bad idea. 
        
if($b != null){
            
$isNotNull=true;
        }
    }
    
    
//If nothing has marked it for saving, delete it
    
if$isNotNull=false){
        unset(
sorted[$aKey]);
    }
}

How can I merge the hr, min and sec keys into time => hr:min:sec?
This one is a tad trickier, you can expand the code above to this

PHP Code:
<?php 
foreach($sorted as $a => $aKey){
    
$isNotNull=false;
    
$isNull=false;
    
//loop though each sub-array
    
foreach($a as $b){
        
// Break isn't a good idea now. As I said before, breaking structured flow is always a bad idea. 
        
if($b != null){
            
$isNotNull=true;
        }
        else{
            
$isNull=true;
        }
    }
    
    
    
//If all values are null, delete
    
if$isNotNull=false){
        unset(
sorted[$aKey]);
    }
    
    
//If nothing is null, we might want to merge the pieces into a string
    
if($isNull==false){
        
reset($a);
        foreach(
$a as $b => $bKey){
            
$tmpArr=array();
            
$matches="";
            
//If it matches this format it might be one of the three keys we are looking for
            
if(preg_match('/^([0-9]+)-([A-z]+)-([0-9]+)$/',$bKey,$matches) !== false){
                
//If it is an hour element
                
if($matches[1]=="hr")
                    
$tmpArr[0]=$b;
                    
                
//If it is a minute element
                
if($matches[1]=="min")
                    
$tmpArr[1]=$b;
                    
                
//If it is a seconds element 
                
if($matches[1]=="sec")
                    
$tmpArr[2]=$b;
            }
            
            
//If the array has a size of 3, it means we found the necessary pieces to have the string
            
if(sizeof($tmpArr==3)){
            
$a[$aKey]["timestring"]=$tmpArr[0] . ':' $tmpArr[1] . ':' $tmpArr[2];
            }
        }
    }
}
I haven't ran this so it might have parse errors and such but the algorithm should work.

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
Reply    


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed