在PHP中动态形成变量名?

The code below dynamically concatenates keys to an existing array $options_pool. So the final form should be: $options_pool[ $base_key ][ $first_key ][ $second_key ]... This is so I can dynamically assign a value to the elements of the array $options_pool which is multidimensional.

foreach( $this->post_vars as $name => $value ) {
    //Look for $name key in array $options_pool if it exists.
    //Use multi_array_key_exists() to handle the task
    //It should return something like "fruit:mango:apple_mango"
    //Then dynamically call $options_pool based on the data. Like so: $options_pool[ 'fruit' ][ 'mango' ][ 'apple_mango' ] = $value;
    $match_key = multi_array_key_exists( $name, $options_pool );
    $option_keys = explode( ':', $match_key );
    $option_keys_length = count( $option_keys );
    $option_name_array = array(); 
    if( 0 < $option_keys_length ) {
     for( $c = $option_keys_length; $c > 0; $c-- ) {
         $sub_keys = '$options_pool';
         for( $c_sub = 0; $c_sub < $c ; $c_sub++ ) {
             $sub_keys .= '[ $option_keys[ '.  $c_sub . ' ] ]';  
         }
         $option_name_array[] = $sub_keys;
     } 
     foreach( $option_name_array as $f_var_name ) {
          //the following line should equal to: $options_pool[ 'fruit' ][ 'mango' ][ 'apple_mango' ] = $value;
          $f_var_name = $value;  
       }
    }
}

//The $options_pool array
$options_pool = array( 'car' => '', 'animals' => '', 'fruit' => array( 'mango' => array( 'apple_mango' => '' ));

I think the logic is correct except that this portion of the code:

foreach( $option_name_array as $f_var_name ) {
    $f_var_name = $value; //particularly this line 
}

doesn't work? I've tested printing the value of $f_var_name and the result is correct but it doesn't really call the array?

That is incorrect, you are right. The variable name will always be $options_pool. You can pass the keys as explode(':', $name) and later assign them.

By the way, your code at

 foreach( $option_keys as $option_keys_value ) {
      $option_key_names[] = $option_keys_value;
 }

Do you realize that it just copies $option_keys as $option_key_names just as if you had written $option_key_names = $option_keys; (in this code) ?

Maybe with a stack you could do this iteratively but with recursion it is just more natural, as you see here

function getVariableToWrite(&$reference, $nested_opts, $write) {
    if(empty($nested_ops)) // Base case
        return $reference = write;
    if(isset($reference[array_shift($nested_ops)]))
        return getVariableToWrite($reference, $nested_ops, $write);
    throw new Exception("The key does not exist..");
}

And then just

foreach( $this->post_vars as $name => $value ) {
    // Your work over here until...
    $match_key = "fruit:mango:apple_mango";
    $option_keys = explode( ':', $match_key );

    getVariableToWrite($options_pool, $option_keys, $value);
}

Will this do the work?

Try this...

foreach( $option_name_array as $key => $f_var_name ) {
    $option_name_array[$key] = $value; //particularly this line 
}

In your foreach, you need to pass the value by reference, so you can edit it.

foreach( $option_name_array as &$f_var_name ){
  $f_var_name = $value;  
}