I'm trying to parse a string with the following structure in PHP:
a,b,c(d,e,f(g),h,i(j,k)),l,m,n(o),p
For example, a "real" string will be:
id,topic,member(name,email,group(id,name)),message(id,title,body)
My end result should be an array:
[
id => null,
topic => null
member => [
name => null,
email => null,
group => [
id => null,
name => null
]
],
message => [
id => null,
title => null,
body => null
]
]
I've tried recursive regex, but got totally lost.
I've got some success with iterating over the string characters, but that seem a bit "over complicated" and I'm sure that is something a regex can handle, I just don't know how.
The purpose is to parse a fields query parameter for a REST API, to allow the client to select the fields he wants from a complex object collection, and I don't want to limit the "depth" of the field selection.
See Question&Answers more detail:
os