Dynamically create multidimensional arrays

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]; } /* $foo structure becomes: Array ( [A-1-001] => A [A] => 1 [1] => 001 ) */ 

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!

+4
source share
4 answers

With some PHP-Fu:

 $matches = array('A-1-001', 'A', 1, '001'); $info = array('some', 'information'); print_r(tree($matches, $info)); function tree($array, $info){ $max = count($array)-1; $result = array($array[$max] => $info); for($i=$max-1;$i>0;$result = array($array[$i--] => $result)); return $result; } 

Output:

 Array ( [A] => Array ( [1] => Array ( [001] => Array ( [0] => some [1] => information ) ) ) ) 
+2
source

Well, this can be done in several ways, for example, using recursion. Now I had another idea in my head. You flip the array with array_reverse . Thus, we can build from the back to the front.

And then we can customize everything. So the code will be as it should

 $foo = array(); $reversed = array_reverse($matches); $some_info_array = array('some', 'information'); foreach($reversed as $key) { if(empty($foo)) { $foo[$key] = $some_info_array; } else { $foo[$key] = $foo; } } 

not tested but should give you the idea of ​​nive / start

+1
source

Given the source array:

 $original_array = array( 'A-1-001', 'A', '1', '001' ); 

You can convert it to the format you described as follows:

 $new_array[ $original_array[1] ] = array( $original_array[2] => $original_array[3] ); 

This gives

 array(1) { ["A"]=> array(1) { [1]=> string(3) "001" } } 

Then you can assign values ​​to this array, as you indicated:

  $new_array['A']['1']['001'] = array('some', 'information'); 
+1
source

You can use such a function ... it should cover any depth. The bonus is that you can continue to pass the value of $ tree to more branches and get one tree with several products.

 function arrayToTree($array, &$tree, $addlAttribs){ $node = array_shift($array); if (count($array)){ // this is a branch $tree[$node] = array(); return arrayToTree($array, $tree[$node]); } else { $tree[$node] = $addlAttribs; return; } } 

Would you use it as

 $t = array(); array_shift($matches); // remove the first match "A-1-001" arrayToTree($matches, $tree, $productInfo); print_r($tree); 
+1
source

Source: https://habr.com/ru/post/1484482/


All Articles