开发者

Regular Expression to split string into nested array

开发者 https://www.devze.com 2023-02-01 09:32 出处:网络
I have this 开发者_如何学运维string: 1[2[5,6]],4,7[8,10] It represents nested IDs to be used to build an array of navigation items.

I have this 开发者_如何学运维string: 1[2[5,6]],4,7[8,10] It represents nested IDs to be used to build an array of navigation items.

I need to parse this string and create a multidimensional array of its values. This will probably take several consecutive expressions and loops to build.

The end product would be an array like this:

1 =>
     2 =>
          5
          6
4
7 => 
     8
     10

I think all I need is an expression that will split the string on commas not inside of a set of brackets. I would then loop over the substrings with the same pattern.


I realize this doesn't directly answer your question, but there's really no point in making your own parser. Just store the data in a JSON string instead.

http://php.net/json-encode

http://php.net/json-decode

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号