I am trying to generate a multidimensional array, with a depth equal to the number of matches found in a regular expression. The array keys to be the string value of each match.
For example:
preg_match('/([A-Z])\-?([0-9])\-?([0-9]{1,3})/i', 'A-1-001', $matches);
Returns:
Array (
[0] => A-1-001
[1] => A
[2] => 1
[3] => 001
)
Which I want to convert to:
$foo = array(
'A' => array(
'1' => array(
'001' => array('some', 'information')
)
)
);
So that I can merge it with another multidimensional array like this:
$bar['A']['1']['001'] = array('some', 'other', 'information');
The process needs to handle any number of matches/dimensions.
Below is my current approach. I'm failing to grasp the concept, because this attempt falls way short of my goal.
$foo = array();
$j = count($matches);
for ($i = 1; $i < $j; $i++) {
$foo[ $matches[$i - 1] ] = $matches[$i];
}
/*
$foo's structure becomes:
Array (
[A-1-001] => A
[A] => 1
[1] => 001
)
*/
It's only swapping array keys, and not creating new children arrays I need.
Any suggestions or solutions would be greatly appreciated. Thanks!