fork download
  1. <?php
  2. $arrayOfString = array(
  3. 0=>'customer service[-3] technical support[-3]',
  4. 1=>'picture quality[+2]feature[+2]',
  5. 2=>'smell[-2]',
  6. 3=>'player[+2]',
  7. 4=>'player[-3][u]',
  8. 5=>'dvd player[-2]',
  9. 6=>'player[+2][p]',
  10. 7=>'format[+2][u]progressive scan[-2]'
  11. );
  12. $out=array();
  13. foreach ($arrayOfString as $k => $v) {
  14. if (preg_match_all('/\b([^\[\]]+?)\[([+-]?\d+)\] */', $v, $matches))
  15. $out[$k] = array_combine ( $matches[1], $matches[2] );
  16. }
  17. print_r($out);
  18. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [customer service] => -3
            [technical support] => -3
        )

    [1] => Array
        (
            [picture quality] => +2
            [feature] => +2
        )

    [2] => Array
        (
            [smell] => -2
        )

    [3] => Array
        (
            [player] => +2
        )

    [4] => Array
        (
            [player] => -3
        )

    [5] => Array
        (
            [dvd player] => -2
        )

    [6] => Array
        (
            [player] => +2
        )

    [7] => Array
        (
            [format] => +2
            [progressive scan] => -2
        )

)