Took a stab at this and came up with the following. Note that this might not work completely for your use case, but it should give you a good starting point. If you're unsure of the $matches
array, just print_r()
it and you'll get a sense for what it is and how it relates to the PCRE.
<?php
$value = '[0][mix][]=on&[0][field]=i.faName&[0][condition]=like&[0][value]=joe&[1][field]=i.enName&[1][condition]=not like&[1][value]=sarah';
$result = [];
$pairs = explode('&', $value);
foreach ($pairs as $pair)
{
preg_match('/^\[(?<objectId>\d+)\]\[(?<name>.*?)\](?<otherArgs>.*?)=(?<value>.*?)$/', $pair, $matches);
if (!isset($result[$matches['objectId']])) {
$result[$matches['objectId']] = [];
}
if (!isset($result[$matches['objectId']][$matches['name']])) {
if ($matches['otherArgs'] == null) {
$result[$matches['objectId']][$matches['name']] = $matches['value'];
} elseif ($matches['otherArgs'] == '[]') {
$result[$matches['objectId']][$matches['name']] = [$matches['value']];
}
} elseif ($matches['otherArgs'] == '[]') {
$result[$matches['objectId']][$matches['name']][] = $matches['value'];
} else {
$result[$matches['objectId']][$matches['name']] = [$result[$matches['objectId']][$matches['name']], $matches['value']];
}
}
print_r($result);
Sample output:
Array
(
[0] => Array
(
[mix] => Array
(
[0] => on
)
[field] => i.faName
[condition] => like
[value] => joe
)
[1] => Array
(
[field] => i.enName
[condition] => not like
[value] => sarah
)
)