2012-03-21 174 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; 
相关问题