2012-08-14 56 views
1

我想將"controllers[page[title_new]]"轉換爲"controllers[page][title_new]"。所以,我可以填寫表格與像輸入字段:表格名稱與陣列

<?php $name = 'controllers[page[title_new]]' ?> 

<input type='text' name='<?= $name; ?>'> 

您提交表格後你會得到$ _ POST =

array(controllers => array(page => array(title_new = ''))); 

我們展示這一點,我想:

<input type='text' name='<?= $name; ?>' value='<?= $_POST[CONVERT($name)]; ?>'> 

那麼在php中是否有這樣一個CONVERT?或者我怎麼能這樣做最好的方式?

*請注意,我離開了所有的驗證/轉義/ヶ輛等,爲了便於閱讀

回答

0

嘛以下作品:

$name = 'controllers[page[title_new]]'; 
$name = str_replace(array('[', ']]'), array('][', ']'), $name); 
//gives: controllers][page][title_new]] -> controllers][page][title_new] 

$name = preg_replace('/\]\[/','[', $name, 1); 
// gives: controllers[page][title_new] 

*我留下了問題,以獲得更好的答案!

0

沒有轉換像你這樣問,但略有不同的方法,可能會更好:

<?php 
$name = 'title_new'; 
$data = $_POST['controllers']['page']; 
?> 
<input type='text' name='controllers[page][<?=$name?>]' value='<?=$data[$name]?>' /> 
+0

對不起,這不會做:(我正在尋找一種通用的方法,因此它可以處理各種控制器,而不僅僅是頁面。 – Tessmore 2012-08-14 16:07:55