split a string into a key => value array in php
Posted
by andy-score
on Stack Overflow
See other posts from Stack Overflow
or by andy-score
Published on 2010-04-19T12:11:17Z
Indexed on
2010/04/19
12:13 UTC
Read the original article
Hit count: 158
+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 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.
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.
© Stack Overflow or respective owner