使用带有\的键在codeigniter中访问数组元素

时间:2018-04-16 11:12:59

标签: codeigniter-3

我的代码是:

public function dirNames()  
{ 
 $maps = directory_map('./application',2);      
 print_r($maps);    
}

我有一个像这样的数组:

Array ( [cache\] => Array ( [0] => index.html ) 
        [config\] => Array ( [0] => autoload.php 
                             [1] => config.php 
                             [2] => constants.php 
                             [3] => database.php 
                             [4] => doctypes.php 
                             [5] => foreign_chars.php 
                             [6] => hooks.php 
                             [7] => index.html 
                             [8] => memcached.php 
                             [9] => migration.php 
                             [10] => mimes.php 
                             [11] => profiler.php 
                             [12] => routes.php 
                             [13] => smileys.php 
                             [14] => user_agents.php ) )

我正在尝试访问这些值并在下一步中将其打印出来: print_r($maps['cache\']);

所以,它显示如下错误:

An uncaught Exception was encountered
Type: ParseError
Message: syntax error, unexpected ''cache\']); ' (T_ENCAPSED_AND_WHITESPACE), expecting ']'
Filename: D:\xampp\htdocs\practice\application\controllers\Practice_controller.php
Line Number: 33
Backtrace:
File: D:\xampp\htdocs\practice\index.php
Line: 315
Function: require_once

我的问题是,我们如何根据此类密钥访问值?请帮帮我。

  

缓存\

1 个答案:

答案 0 :(得分:0)

希望这会对您有所帮助:

通过添加backslash('\')来访问它们,如下所示:

$maps = directory_map('./application',2); 

/* for cache*/ 
print_r($maps['cache\\']);

/* for config*/
print_r($maps['config\\']);