fork download
  1. <?php
  2. $string = 'Primary[Blue|0000FF,Red|FF0000,Yellow|FFFF00],Secondary[Green|00FF00,Orange|FF9900,Purple|663399],Brown|A52A2A,Silver|C0C0C0';
  3.  
  4. $array = preg_split('/(?![^\[]+\]),/', $string);
  5.  
  6. $return = array();
  7.  
  8. foreach($array as $val){
  9. if(preg_match('/(.*)\[(.*)\]/', $val, $match) === 1){
  10. $return[$match[1]] = explode(',', $match[2]);
  11. }
  12. else{
  13. $return[] = $val;
  14. }
  15. }
  16.  
  17. var_dump($return);
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
array(4) {
  ["Primary"]=>
  array(3) {
    [0]=>
    string(11) "Blue|0000FF"
    [1]=>
    string(10) "Red|FF0000"
    [2]=>
    string(13) "Yellow|FFFF00"
  }
  ["Secondary"]=>
  array(3) {
    [0]=>
    string(12) "Green|00FF00"
    [1]=>
    string(13) "Orange|FF9900"
    [2]=>
    string(13) "Purple|663399"
  }
  [0]=>
  string(12) "Brown|A52A2A"
  [1]=>
  string(13) "Silver|C0C0C0"
}