PHP foreach数组只返回第一个字符

时间:2018-04-06 12:33:39

标签: php

我在会话中有一个如下所示的数组:

Array
(
    [0] => Array
        (
            [0] => /storage/ssd3/334/5218334/tmp/php2swaoM
            [1] => Petty cash request form.png
            [2] => ../images/Petty cash request form.png
        )

    [1] => Array
        (
            [0] => /storage/ssd3/334/5218334/tmp/phpISXPED
            [1] => Business Advance Form.png
            [2] => ../images/Business Advance Form.png
        )
)

问题是,当我循环遍历数组时,它只返回第一个字符,这是我的代码:

  $array = $_SESSION["att_arr"];
  $requestID =  $_SESSION["requestID"];

  foreach ($array as $key => $values) {
    foreach ($values as $value) {
        $filetmp = $value[0];
        $filename = $value[1];
        $filepath = $value[2];

        //move_uploaded_file($filetmp,$filepath);

        $insert_sql = "INSERT INTO attachments (name, location, request_id) VALUES ('$filename','$filepath','$requestID')";

        if(mysqli_query($dbcon, $insert_sql)){
          echo "done";
        } else{
          echo "ERROR: Could not able to execute $insert_sql. " . mysqli_error($dbcon);
        }
    }
  }

1 个答案:

答案 0 :(得分:2)

这循环内部数组中的每个项目:

foreach ($values as $value) {
    $filetmp = $value[0];

例如,在第一个循环中,$value/storage/ssd3/334/5218334/tmp/php2swaoM。当你执行$value[0]时,它将返回第一个字符。

这就是你做错了。

我相信如果所有内部数组都有固定的3个项目,你可以省略内部循环。