我如何遍历这个 php 数组
How can I iterate over this php array
我有这个变量,它 INSERTs
到数据库 html 表单输入的附加文件名。
$insertAttachments->execute( array(':attachment2' => $attachment2) );
现在的问题是 INSERTs
虽然输入设置为允许多个,但只有一个文件名。所以我试着这样做:
$uploads = count($attachment2); //counts the number of attachments
for($i=0; $i<$uploads; $i++){
$insertAttachments->execute( array(':attachment2' => $attachment2) );
}
这让我想知道在哪里放置 [$i]
以便迭代 array(':attachment2' => $attachment2)
?
如果我给它分配一个变量并让它像 -
$alluploads = array(':attachment2' => $attachment2);
for($i=0; $i<$uploads; $i++){
$insertAttachments->execute( $alluploads[$i] );
}
遇到错误。我应该如何解决这个问题?我正在扩展 php 5.3 / slim 框架应用程序。
Foreach 让您遍历关联数组。
$alluploads = array(':attachment2' => "attachment2");
foreach($alluploads as $key => $value){
echo "key: " . $key . " Has value: ". $value ."\n";
}
这是一个使用 array_keys 获取关联数组键并在循环中使用它的 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";
}
作为第三个示例,您可以使用 array_values 删除关联键。
仅当您知道数组中的所有值都是上传的并且应该被同等对待时才应使用此方法。
$alluploads = array(':attachment2' => "attachment2");
$alluploads = array_values($alluploads);
for($i=0;$i<count($alluploads);$i++){
echo "key: " . $i . " Has value: ". $alluploads[$i] ."\n";
}
您可以使用 foreach
循环,这样就不需要计数,如下所示:
foreach ($attachment2 as $a){
$insertAttachments->execute( array(':attachment2' => $a['some index with the file path']) );
}
别忘了更改数组的索引
我有这个变量,它 INSERTs
到数据库 html 表单输入的附加文件名。
$insertAttachments->execute( array(':attachment2' => $attachment2) );
现在的问题是 INSERTs
虽然输入设置为允许多个,但只有一个文件名。所以我试着这样做:
$uploads = count($attachment2); //counts the number of attachments
for($i=0; $i<$uploads; $i++){
$insertAttachments->execute( array(':attachment2' => $attachment2) );
}
这让我想知道在哪里放置 [$i]
以便迭代 array(':attachment2' => $attachment2)
?
如果我给它分配一个变量并让它像 -
$alluploads = array(':attachment2' => $attachment2);
for($i=0; $i<$uploads; $i++){
$insertAttachments->execute( $alluploads[$i] );
}
遇到错误。我应该如何解决这个问题?我正在扩展 php 5.3 / slim 框架应用程序。
Foreach 让您遍历关联数组。
$alluploads = array(':attachment2' => "attachment2");
foreach($alluploads as $key => $value){
echo "key: " . $key . " Has value: ". $value ."\n";
}
这是一个使用 array_keys 获取关联数组键并在循环中使用它的 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";
}
作为第三个示例,您可以使用 array_values 删除关联键。
仅当您知道数组中的所有值都是上传的并且应该被同等对待时才应使用此方法。
$alluploads = array(':attachment2' => "attachment2");
$alluploads = array_values($alluploads);
for($i=0;$i<count($alluploads);$i++){
echo "key: " . $i . " Has value: ". $alluploads[$i] ."\n";
}
您可以使用 foreach
循环,这样就不需要计数,如下所示:
foreach ($attachment2 as $a){
$insertAttachments->execute( array(':attachment2' => $a['some index with the file path']) );
}
别忘了更改数组的索引