Solved

Sorting elements from string

Posted on 2011-09-29
6
261 Views
Last Modified: 2012-05-12
Hi Experts!

I've got a string like so:
$str = '2009,(1)110,(6)612,(7)718,(20)2416,(25)3058,(30)3849,2010,(4)431,(7)876,(17)2058,(42)5036,(44)5248,(51)6182,2011,(12)1244,(13)1447,(34)3716';

Open in new window

And I need to sort it, so the final output would be sort by year (four digits between commas, no brakets in above string) and numbers with brakets would be moved from front to the back of the main number, so result should look like this:
$y2009 = '110(1),612(6),718(7),2416(20),3058(25),3849(30)';
$y2010 = '431(4),876(7),2058(17),5036(42),5248(44),6182(51)';
$y2011 = '1244(12),1447(13),3716(34)';

Open in new window

I probably need to play with 'explode' and 'preg_replace' functions, but not sure how, please help.

Thanks.
0
Comment
Question by:Zado
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36813748
I'll post back in a second
but here's what I have so far:

<?php

    error_reporting(E_ERROR);

    function trimch($str){
        return trim($str,",");
    }

    function multipleExplode($delimiters = array(), $string = ''){ 

         $mainDelim=$delimiters[count($delimiters)-1]; // dernier 
         
         array_pop($delimiters); 
         
         foreach($delimiters as $delimiter){ 
         
             $string= str_replace($delimiter, $mainDelim, $string); 
         
         } 

         $result= explode($mainDelim, $string); 
         return $result; 

     }

	$str = '2009,(1)110,(6)612,(7)718,(20)2416,(25)3058,(30)3849,2010,(4)431,(7)876,(17)2058,(42)5036,(44)5248,(51)6182,2011,(12)1244,(13)1447,(34)3716';
	
    $pattern = '/^\d{4}|\,\d{4}/';
    preg_match_all($pattern, $str, $matches);
    
    // here we explode by year
    $test = multipleExplode($matches[0], $str);
    
    // remove commas
    $years = array_map(trimch,$matches[0]);
    
    //trim commas from exploded strings
    $strs = array_map(trimch,$test);
    
    print_r($matches);
    print_r($strs);
    
?>

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 36813792
Ok, I will wait for final result, thanks for your help!
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36813828
Ok getting close:

<?php

    error_reporting(E_ERROR);

    function trimch($str){
        return trim($str,",");
    }

    function multipleExplode($delimiters = array(), $string = ''){ 

         $mainDelim=$delimiters[count($delimiters)-1]; // dernier 
         
         array_pop($delimiters); 
         
         foreach($delimiters as $delimiter){ 
         
             $string= str_replace($delimiter, $mainDelim, $string); 
         
         } 

         $result= explode($mainDelim, $string); 
         return $result; 

     }

	$str = '2009,(1)110,(6)612,(7)718,(20)2416,(25)3058,(30)3849,2010,(4)431,(7)876,(17)2058,(42)5036,(44)5248,(51)6182,2011,(12)1244,(13)1447,(34)3716';
	
    $pattern = '/^\d{4}|\,\d{4}/';
    preg_match_all($pattern, $str, $matches);
    
    // here we explode by year
    $test = multipleExplode($matches[0], $str);
    // remove the first empty element
    array_shift($test);
    
    // remove commas
    $years = array_map(trimch,$matches[0]);
    
    //trim commas from exploded strings
    $strs = array_map(trimch,$test);
    
    //print_r($years);
    //print_r($strs);
    
    $values = array();
    
    // let's sort the strings
    foreach($strs as $val){
        
        // explode the string with ,
        $array = explode(",",$val);
        
        $line = "";
        
        // reverse the bracket number with the number
        foreach($array as $piece){
            
            $tmp_string = explode(")",$piece);
            $tmp_string = $tmp_string[1].$tmp_string[0]."),";
            $line .= $tmp_string;
            
        }
        $line = trim($line,",");        
        $values[] = $line;
    }
    
    $result = array_combine($years,$values);
    
    print_r($result);
    
?>

Open in new window

0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 36813840
No matter what you do with the program code, you are going to come up with a brittle design.  This almost looks like XML with the markup removed.  I think I might not go the extra step to assigning local variables.  Since the data appears to be related by year it would make more sense to me to keep it in an array.  You might rtrim() the array values.
http://www.laprbass.com/RAY_temp_zado.php

Outputs something like
Array
(
    [2009] => (1)110,(6)612,(7)718,(20)2416,(25)3058,(30)3849,
    [2010] => (4)431,(7)876,(17)2058,(42)5036,(44)5248,(51)6182,
    [2011] => (12)1244,(13)1447,(34)3716,
)
string(47) "(1)110,(6)612,(7)718,(20)2416,(25)3058,(30)3849"
string(49) "(4)431,(7)876,(17)2058,(42)5036,(44)5248,(51)6182"
string(26) "(12)1244,(13)1447,(34)3716"

HTH, ~Ray
<?php // RAY_temp_zado.php
error_reporting(E_ALL);
echo "<pre>";

// ALLOW THE USE OF STRTOTIME() TO DETECT YEARS
date_default_timezone_set('America/Chicago');

// THE TEST DATA
$str = '2009,(1)110,(6)612,(7)718,(20)2416,(25)3058,(30)3849,2010,(4)431,(7)876,(17)2058,(42)5036,(44)5248,(51)6182,2011,(12)1244,(13)1447,(34)3716';

// BREAK THE TEST DATA BY REMOVING COMMAS
$arr = explode(',', $str);

// SET A SIGNAL STRING
$yrs = '?';

// USE AN ITERATOR TO COVER THE ARRAY
foreach ($arr as $num)
{
    // IF IT LOOKS LIKE A YEAR
    if (strtotime($num))
    {
        // START A NEW ELEMENT OF THE ARRAY
        $out[$num] = NULL;
        $yrs = $num;
    }
    else
    {
        // APPEND TO THIS ELEMENT OF THE ARRAY
        $out[$yrs] .= $num . ',';
    }
}

// SHOW THE WORKING ARRAY
print_r($out);

// CONVERT THE ARRAY INTO LOCAL STRINGS
foreach ($out as $yrs => $nms)
{
    // CREATE A VARIABLE NAME AND ASSIGN THE STRING
    $ndx = 'y' . $yrs;
    $$ndx = rtrim($nms, ',');
}

// SHOW THE LOCAL STRINGS BY NAME
var_dump($y2009);
var_dump($y2010);
var_dump($y2011);

Open in new window

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 400 total points
ID: 36813841
<?php

    error_reporting(E_ERROR);

    function trimch($str){
        return trim($str,",");
    }

    function multipleExplode($delimiters = array(), $string = ''){ 

         $mainDelim=$delimiters[count($delimiters)-1]; // dernier 
         
         array_pop($delimiters); 
         
         foreach($delimiters as $delimiter){ 
         
             $string= str_replace($delimiter, $mainDelim, $string); 
         
         } 

         $result= explode($mainDelim, $string); 
         return $result; 

     }

	$str = '2009,(1)110,(6)612,(7)718,(20)2416,(25)3058,(30)3849,2010,(4)431,(7)876,(17)2058,(42)5036,(44)5248,(51)6182,2011,(12)1244,(13)1447,(34)3716';
	
    $pattern = '/^\d{4}|\,\d{4}/';
    preg_match_all($pattern, $str, $matches);
    
    // here we explode by year
    $test = multipleExplode($matches[0], $str);
    // remove the first empty element
    array_shift($test);
    
    // remove commas
    $years = array_map(trimch,$matches[0]);
    
    //trim commas from exploded strings
    $strs = array_map(trimch,$test);
    
    //print_r($years);
    //print_r($strs);
    
    $values = array();
    
    // let's sort the strings
    foreach($strs as $val){
        
        // explode the string with ,
        $array = explode(",",$val);
        
        $line = "";
        
        // reverse the bracket number with the number
        foreach($array as $piece){
            
            $tmp_string = explode(")",$piece);
            $tmp_string = $tmp_string[1].$tmp_string[0]."),";
            $line .= $tmp_string;
            
        }
        $line = trim($line,",");        
        $values[] = $line;
    }
    
    $result = array_combine($years,$values);
    
    //print_r($result);
    
    foreach($result as $key => $val){
        $str = "y".$key;
        $$str = $val;
        
        echo"the variable $str equals $val<br />";
    }
    
    // AND JUST TO MAKE SURE:
    echo $y2009."<br />";
    echo $y2010."<br />";
    echo $y2011."<br />";
    
?>

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 36813966
Awesome, Roads_Roads script works perfectly! Ray, thanks for your help.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question