将字符串添加到数组,保持为数组

时间:2013-05-13 23:56:51

标签: php arrays string

我有两个数组:

$delete = array("Delete" => "Delete");

$folders = array("0" => "folder1","1" => "folder2");

$options = array_merge($delete, $folders);

如何将“移至”添加到$文件夹并将$ options保留为数组?

在下拉菜单中,它将显示为:

Delete

Move to folder1

Move to folder2

3 个答案:

答案 0 :(得分:1)

如果要将“Move to”字符串添加到$ folder数组的每个元素,请尝试以下操作:

$folders = array("0" => "folder1","1" => "folder2");
foreach($folders as $key => $value) {
    $folders[$key] = 'Move to '.$value;
}
$options = array_merge($delete, $folders);

OR(基于@zamnuts的建议)

$folders = array("0" => "folder1","1" => "folder2");

function moveto($n) {  
     return 'Move to '.$n; 
}
$folders = array_map("moveto", $folders);
$options = array_merge($delete, $folders);

答案 1 :(得分:0)

这应该做你需要的:

$folders[] = "Move to";

$options = array_merge($delete, $folders);

答案 2 :(得分:0)

不确定是否存在真实问题,但只需在array_merge() $folders[] = "Move to"之后添加{%}}。由于array_merge()生成了一个新数组,因此对$delete$folders之后的任何修改都不会影响$options的内容。