2013-07-12 103 views
0

在PHP腳本中,我想編輯一個節點及其屬性。PHP-SimpleXML:編輯節點及其屬性

<config> 
    <Week> 
     <Monday from="18:00:00" to="8:00:00">true</Monday> 
     <Tuesday from="18:00:00" to="8:00:00">true</Tuesday> 
</Week></config> 


foreach ($config->Week->children() as $day){ 
    $day = $_POST["b_".$day]; 
    $day['from'] = $_POST[$day."_from"]; 
    $day['to'] = $_POST[$day."_to"]; 
} 

這似乎並沒有工作。但這並

$config->Week->Monday = $_POST['b_Monday']; 
$config->Week->Monday['from'] = $_POST['Monday_from']; 
$config->Week->Monday['to'] = $_POST['Monday_to']; 

有沒有辦法做到這一點在一個循環中,所以我在沒有硬編碼的節點名稱?從日

+0

推薦閱讀:[如何在PHP中獲取有用的錯誤消息?](http://stackoverflow.com/q/845021/367456) – hakre

回答

0

變化& $日

foreach ($config->Week->children() as &$day){ 

但是,下一個是更好

$children = $config->Week->children(); 
foreach ($children as &$day) { 
     : 
     : 
} 
$config->Week->setChildren($children); // setter 

它不能有太大的英語,但我會流傳下來的?

+0

沒有通過引用讓日變量不起作用在foreach循環中 – user2367868