0

I Have an array in a session that looks like this:

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
        )
)

The problem is that when i loop through the array it is returning only the first character here is my code:

  $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);
        }
    }
  }
3
  • 6
    take out the second foreach, change references to $value to $values Commented Apr 6, 2018 at 12:35
  • 5
    I come here to answer questions and offer advice, not sure what your agenda is Commented Apr 6, 2018 at 12:36
  • Earlier duplicate with a less clear minimal reproducible example: creating multidimensional array from single array Commented Mar 11 at 10:11

1 Answer 1

2

This is looping every item in the inner arrays:

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

In the first loop, for example, $value is /storage/ssd3/334/5218334/tmp/php2swaoM. When you do $value[0], it'll return the first character.

That's what you did wrong.

I believe you can omit the inner loop if all inner arrays will have a fixed 3 items.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.