如何在方括号(PHP)中拆分字符串中的数组?

How to split array in string in square bracket (PHP)?

表单信息是这个字符串[1,1x,1x,1x][2,2x,2x,2x]这是两个行号。

这适用于 PHP 5.4+ 我有 5.2:

$str = "[1,1x,1x,1x][2,2x,2x,2x]";
$arr = [];
preg_replace_callback('/\[(.*?)\]/', function($m) use (&$arr){
    $arr[] = explode(',',preg_replace('/([\w]+)/', "[]", $m[1]));
}, $str);

这是输出:

Array {

  [0]=>Array {
      [0]=> "[1]" //rownumber
      [1]=> "[1x]"
      [2]=> "[1x]"
      [3]=> "[1x]"
  }
  [1]=>Array {
      [0]=> "[2]" //rownumber
      [1]=> "[2x]"
      [2]=> "[2x]"
      [3]=> "[2x]"
  }
  //etc
  [3]=>Array {
      [0]=> "[3]" //rownumber
      [1]=> "[3x]"
      [2]=> "[3x]"
      [3]=> "[3x]"
  }

}

Working demo

您可以像

一样简单地使用preg_replace_callbackexplode
$str = "[1,1x,1x,1x][2,2x,2x,2x]";
$arr = [];
preg_replace_callback('/\[(.*?)\]/', function($m) use (&$arr){
    $arr[] = explode(',',preg_replace('/([\w]+)/', "[]", $m[1]));
}, $str);

Working Demo