views:

357

answers:

1
+2-1+18*+7-21+3*-4-5+6x29

The above string is an example of the kind of string I'm trying to split into either a key => value array or something similar. The string is used to represent the layout of various classes on a three column page of an intranet site, which is editable by the user via drag and drop. This string is stored in a cookie to be used on the next visit.

The numbers represent the id of a class and -,+ and x represent the state of the class (minimised, expanded or hidden), the * represents a column break.

I can split this into the columns easily using explode which gives and array with 3 $key => $value associations.

eg.

$column_layout = array( [0] => '+2-1+18' , [1] => '+7-21+3' , [2] => '-4-5+6x29' )

I then need to split this into the various classes from there, keeping the status and id together. As the different classes and statuses will change from user to user and how many there are for each column, I need to be able to do this all automatically.

eg.

$column1 = array( '+' => 2 , '-' => 1 , '+' => 18 )
...

or

$column1 = array( array( '+' , 2 ) , array( '-' , 1 ) , array( '+' , 18 ) )
...

I can't quite get my head round this and what the best way to do it is, so any help would be much appreciated.

+5  A: 

First explode() the array with the delimiter *

You could then use preg_match_all to match each item in the exploded array. Something like this works with your example input.

$layout = explode('*', $input);
$columns = array();
foreach ( $layout as $item ){
    $parts = array();

    //matches either a -, x or + followed by one or more digits
    preg_match_all('/([+-x])(\d+)/', $item, $matches, PREG_SET_ORDER);

    foreach ( $matches as $match){ 
        //match[1] hold the + or -, match[2] holds the digits
        $parts[] = array($match[1], $match[2]);
    }
    $columns[] = $parts;
}

The output from your example ends up like this:

array(
     array( array('+', '2'), array('-', '1'), array('+', '18') ),
     array( array('+', '7'), array('-', '21'), array('+', '3') ),
     //etc
);

With PHP 5.3 you could use something like this (untested). The main difference is that the inner loop has been replaced by array_map which removes the need for a lot of lines of code. (Array map applies a function to every item in an array and returns the transformed array). PHP 5.3 is required for the nice closure syntax

$layout = explode('*', $input);
$columns = array();
foreach ( $layout as $item ){
    preg_match_all('/([+-x])(\d+)/', $item, $matches, PREG_SET_ORDER);
    $columns[] = array_map( function($a){ return array($a[1], $a[2]); },
                            $matches);
}

You could also remove the loops altogether:

$innerMatch = function($item){
    preg_match_all('/([+-x])(\d+)/', $item, $matches, PREG_SET_ORDER);
    return array_map( function($a){ return array($a[1], $a[2]); },
                      $matches);
};
$columns = array_map($innerMatch, explode('*', $input));

However this has the large disadvantage of not being very readable to most PHP developers which is why I wouldn't recommend using it.


More explanation

At the request of @Christopher Altman

The only new bit in the PHP 5.3 version is really this:

array_map(
          function($a){ return array($a[1], $a[2]); },
          $matches
);

Expanding and altering it a bit (as an example)

//bind an anonymous function to the variable $func
$func = function($a){
    return $a*$a; 
}; 
//$func() now calls the anonymous function we have just defined

//then we can call it like so:
$result = array_map($func, $myArray);

So if $myArray is defined as

array(1,2,3,4);

When it is run through the array map function you can think of it as converting it into

array(func(1),func(2),func(3),func(4));

But as PHP isn't a lazy language, all the functions are evaluated as soon as they are encountered, so the array is returned from array_map as:

array(2, 4, 9, 16)

In the actual code, preg_match_all returns an array of matches (where the matches are arrays). So all I do is take the array and on every match apply a function that converts the match into a different array in the required format.

Yacoby
Could you explain how the PHP 5.3 example is working. It looks powerful, just want a quick breakdown on each piece.
Christopher Altman
Excellent, thank you. This is just what I needed.I'd tried using preg_split but that got rid of the delimiter, which made things complex.
andy-score
@Christopher I added some more explanation
Yacoby