2016-08-22 40 views
0

使用file_get_contents我能得到一个树枝文件的内容,其中只包含一个多维数组:转换嫩枝数组PHP数组

嫩枝:

{% set settings = { 
    theme : '#91141b', 
    transforms : { 
    thumb : { mode:'crop', width:300, height:300, quality:80, position:'center-center'}, 
    small : { mode:'fit', width:100, height:100, quality:70, position:'center-center'}, 
    header : { mode:'fit', height:800, quality:80, position:'center-center'}, 
    mobile : { mode:'crop', width:640, height:1136, quality:60, position:'center-center'} 
    }, 
    globals : { 
    telephone : '123456678', 
    address : '42 Wallaby Way, Sydney' 
    }, 
    social : { 
    facebook : 'http://www.facebook.com', 
    twitter : 'http://www.twitter.com', 
    linkedin : 'http://www.linkedin.com', 
    youtube : 'http://www.youtube.com' 
    } 
} %} 

在PHP中,我现在有这个字符串玩:

{% set settings = { theme : '#91141b', transforms : { thumb : { mode:'crop', width:300, height:300, quality:80, position:'center-center'}, small : { mode:'fit', width:100, height:100, quality:70, position:'center-center'}, header : { mode:'fit', height:800, quality:80, position:'center-center'}, mobile : { mode:'crop', width:640, height:1136, quality:60, position:'center-center'} }, globals : { telephone : '123456678', address : '42 Wallaby Way, Sydney' }, social : { facebook : 'http://www.facebook.com', twitter : 'http://www.twitter.com', linkedin : 'http://www.linkedin.com', youtube : 'http://www.youtube.com' } } %} 

有没有办法把它变成一个可用的PHP多维数组?就像这样:

PHP:

$settings = array(
    'theme' => '#91141b', 
    'transforms' => [ 
    'thumb' => [ 'mode'=>'crop', 'width'=>300, 'height'=>300, 'quality'=>80, 'position'=>'center-center'], 
    'small' => [ 'mode'=>'fit', 'width'=>100, 'height'=>100, 'quality'=>70, 'position'=>'center-center'], 
    'header' => [ 'mode'=>'fit', 'height'=>800,'quality'=>80, 'position'=>'center-center'], 
    'mobile' => [ 'mode'=>'crop', 'width'=>640, 'height'=>1136,'quality'=>60, 'position'=>'center-center'] 
    ], 
    'globals' => [ 
    'telephone' => '123456678', 
    'address' => '42 Wallaby Way, Sydney' 
    ], 
    'social' => [ 
    'facebook' => 'http://www.facebook.com', 
    'twitter' => 'http://www.twitter.com', 
    'linkedin' => 'http://www.linkedin.com', 
    'youtube' => 'http://www.youtube.com' 
    ] 
); 

我不知道是否有一个解析器函数,我不知道的。

感谢,

马克

+0

为什么要将树枝数组提取到PHP中? – DarkBee

回答

0

好吧,我有点理解了它。

在树枝文件,渲染出具有json_encode过滤器变量的设置:

{{ settings|json_encode|raw }} 
在你的PHP

然后插件,您可以在树枝文件渲染和使用PHP的json_decode

$file = '_includes/settings'; 

$settings = json_decode(craft()->templates->render($file), true);