2016-08-22 56 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);