Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    84
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Array implosion question

    Let's say I have an array with values 1,2,3,5,7 or 1,3,4,5 and I wanted to change this to a string that would read 1-3,5,7 or 1,3-5. I am currently at a loss of ideas on how to accomplish this. If someone could just throw up some psuedo code I could probably go from there.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    PHP Code:
    <?php
    /**
      signature
        array getRanges( array )

      splits up the array into integer ranges, where each range is an array with the start and end of a range as values (in this order)
    */
    function getRanges($array)
    {
        
    reset($array);
        
        
    $ranges = array();
        
        if(
    FALSE !== key($array))
        {
            
    $rStart $rEnd current($array);
            
    $cur next($array);
            
            while(
    NULL !== key($array))
            {
                if(
    $cur != $rEnd 1)
                {
                    
    $ranges[] = array($rStart$rEnd);
                    
    $rStart $rEnd $cur;
                }
                else
                {
                    ++
    $rEnd;
                }
                
    $cur next($array);
            }
            
            
    $ranges[] = array($rStart$rEnd);
        }
        return 
    $ranges;
    }
    /**
      signature
        string stringRanges( array )

      returns a string representation of the integer ranges of the values in the given array
    */
    function stringRanges($array)
    {
        
    $str '';
        foreach(
    getRanges($array) as $range)
        {
            
    $str .= (($range[0] == $range[1]) ?
                
    $range[0] :
                
    $range[0].'-'.$range[1]).',';
        }
        return 
    substr($str0, -1);
    }

    $array1 = array(1,2,3,5,7,8,9,10,12);

    echo 
    'array('.implode(','$array1).') = ';
    echo 
    stringRanges($array1);
    ?>
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • Users who have thanked dumpfi for this post:

    Bobafart (01-04-2008)

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    function get_ranges$array )
    {
        
    sort$array );
        
    $key 0;
        
    $ranges[$key][] = reset$array );
        while ( 
    $curr next$array ) )
        {
            
    $prev prev$array );
            
    next$array );
            if ( 
    $curr == $prev )
            {
                
    $ranges[$key][] = $curr;
            }
            else
            {
                
    $ranges[++$key][] = $curr;
            }
        }
        
    $return = array();
        foreach ( 
    $ranges as $k => $v )
        {
            
    $return[$k] = reset$v );
            if ( 
    count$v ) > )
            {
                
    $return[$k] .= '-' end$v );
            }
        }
        return 
    implode', '$return );
    }

    $array = array( 12356891011121618192025 );

    echo 
    get_ranges$array ); // 1-3, 5-6, 8-12, 16, 18-20, 25 
    Last edited by kbluhm; 01-03-2008 at 06:21 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •