douxigai8757 2013-04-18 14:59
浏览 32
已采纳

将config ini转换为字符串点分隔值到实际数组键

got a multidimensional array and a string in a config and i need to transform it as an array key without the use of eval. Real world use of this problems is that i got a big document from mongodb that is transformed into multi dimensional array. However i need to define specific array nodes from a config file.

the idea is to create a config file as representation of the array key's hierarchy

on the config.ini the values below are some example.

colorattribute = attribute.color
wholesaleprice = prices.wholesale

Example Response from mongoDb

<?php
$products = array(
                'product_name' => 'iTouch',
                'brand_name' => 'Apple',
                'attributes' => array ( 'color' => 'black',
                                        'size' => '5 in'
                                      ),
                'prices' => array(
                                    'wholesale' => 135,
                                    'retail' => 200,
                                ),
                );
  • 写回答

1条回答 默认 最新

  • dousongxuan7507 2013-04-18 15:04
    关注
     function recurseKeys(array $keys,array $array){
        $key = array_shift($keys);
        if(!isset($array[$key])) return null;
        return empty($keys) ?
          $array[$key]:
          recurseKeys($keys,$array[$key];
     }
    
     var_dump(recurseKeys(explode('.',$testConfig),$products);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?