du131642 2010-12-14 18:37
浏览 8
已采纳

在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?

  • 写回答

3条回答 默认 最新

  • dpb_4431 2010-12-14 18:51
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据