2012-03-21 171 views
1

我知道我可以這樣添加elemnts到一個數組:使用PHP中的鍵/值將元素添加到數組中?

$arr = array("foo" => "bar", 12 => true); 

現在,我怎麼能做到這一點在foreach使用動態值?我有這樣的代碼:

foreach ($values as $value) { 

    $imagePath = $value->getImagePath(); 
    $dependsOn = $value->getDependsOn(); 
    $dependsOn = explode(':', $dependsOn); 
    $dependsOnOptionValueTitle = trim($dependsOn[1]); 

    array_push($paths, $dependsOnOptionValueTitle => $imagePath); // not working 
} 

如何,我可以添加鍵/值對我$paths陣列?

+0

http://php.net/array – hakre 2012-07-01 13:46:43

回答

2

從我所看到的,這是你想要做什麼:

$paths[$dependsOnOptionValueTitle] = $imagePath; 

評論,如果我錯了,我會嘗試修復它。

+0

沒錯,那正是我想要的,非常感謝! – EOB 2012-03-21 12:25:47

3

而不是

array_push($paths, $dependsOnOptionValueTitle => $imagePath); // not working 

,你應該能夠使用

$paths[$dependsOnOptionValueTitle] = $imagePath; 
相關問題