如何迭代这个php数组

如何迭代这个php数组,php,arrays,iteration,slim,Php,Arrays,Iteration,Slim,我有一个变量,将html表单输入中附加的文件名插入数据库 $insertAttachments->execute( array(':attachment2' => $attachment2) ); 现在的问题是它只插入一个文件名,尽管输入设置为允许多个文件名。所以我试着这样做: $uploads = count($attachment2); //counts the number of attachments for($i=0; $i<$uploads; $i++){

我有一个变量,
将html表单输入中附加的文件名插入数据库

$insertAttachments->execute( array(':attachment2' => $attachment2) );
现在的问题是它只插入一个文件名,尽管输入设置为允许多个文件名。所以我试着这样做:

$uploads = count($attachment2);  //counts the number of attachments
for($i=0; $i<$uploads; $i++){
    $insertAttachments->execute( array(':attachment2' => $attachment2) );
}

它遇到了一个错误。你知道我该怎么做吗?我正在扩展一个PHP5.3/slim框架应用程序。

Foreach让我们在关联数组上进行迭代

$alluploads = array(':attachment2' => "attachment2");
foreach($alluploads as $key => $value){
    echo "key: " . $key . " Has value: ". $value ."\n";
}

下面是一个示例,其中for循环使用数组_键获取关联数组键,并在循环中使用该键

$alluploads = array(':attachment2' => "attachment2");
$keys = array_keys($alluploads);
for($i=0;$i<count($keys);$i++){
    echo "key: " . $keys[$i] . " Has value: ". $alluploads[$keys[$i]] ."\n";
}
$alluploads=array(':attachment2'=>“attachment2”);
$keys=数组_键($ALUPLOADS);
对于($i=0;$i“附件2”);
$alluploads=数组_值($alluploads);

对于($i=0;$i可以使用
foreach
循环,这样就不需要计数,如下所示:

foreach ($attachment2 as $a){
    $insertAttachments->execute( array(':attachment2' => $a['some index with the file path']) );
}

别忘了更改数组的索引

Use foreach而不是您似乎误解了非常基本的PHP、数组和循环功能。我建议您阅读一些有关该主题的介绍教程。您是否知道第二个示例$alluploads!=第三个示例中的$uploadsSo在
for
循环中有拼写错误我说的
$insertAttachments->execute(数组(':attachment2'=>$alluploads[$I]);
正确吗?因为
$alluploads[$I]
返回了attachment2的值?不管怎样,我只是尝试了
执行(数组(':attachment2'=>$alluploads[$I])
执行(数组($alluploads[$I]))
它们都能工作。谢谢。@Clint\u我之所以将它们按顺序排列,是因为foreach是最好的答案,因为with array\u键是可以接受的答案。array\u值可以工作,但不应该真正使用。
$alluploads = array(':attachment2' => "attachment2");
$alluploads = array_values($alluploads);
for($i=0;$i<count($alluploads);$i++){
    echo "key: " . $i . " Has value: ". $alluploads[$i] ."\n";
}
foreach ($attachment2 as $a){
    $insertAttachments->execute( array(':attachment2' => $a['some index with the file path']) );
}