I am trying to create a multidimensional array with a depth equal to the number of matches found in the regular expression. Array keys must be the string value of each match.
For instance:
preg_match('/([AZ])\-?([0-9])\-?([0-9]{1,3})/i', 'A-1-001', $matches);
Return:
Array ( [0] => A-1-001 [1] => A [2] => 1 [3] => 001 )
What I want to convert to:
$foo = array( 'A' => array( '1' => array( '001' => array('some', 'information') ) ) );
So, I can combine it with another multidimensional array as follows:
$bar['A']['1']['001'] = array('some', 'other', 'information');
The process must handle any number of matches / dimensions.
Below is my current approach. I do not understand the concept, because this attempt is not in line with my goal.
$foo = array(); $j = count($matches); for ($i = 1; $i < $j; $i++) { $foo[ $matches[$i - 1] ] = $matches[$i]; }
This is just a replacement for the array keys, not the creation of the new child arrays that I need.
Any suggestions or solutions are welcome. Thanks!
Jason source share