-1

I am unable to get as to why I am unable to change the value of an array of an array. Here's the code I tried

print_r($target_file);
foreach ($target_file as $k => $v) {
   unset ($v['tmp_name']);
   print_r($v);
   $v['tmp_name'] = "joker";
   print_r($v);
}

print_r($target_file);

And output I get is:

(
    [0] => Array
        (
            [name] => order-confirmation-60dc3f6f8d7f3.pdf
            [type] => application/pdf
            [tmp_name] => C:\Users\kbged\Desktop\XAMPP\tmp\php2FDB.tmp
            [error] => 0
            [size] => 367185
        )

)
Array
(
    [name] => order-confirmation-60dc3f6f8d7f3.pdf
    [type] => application/pdf
    [error] => 0
    [size] => 367185
)
Array
(
    [name] => order-confirmation-60dc3f6f8d7f3.pdf
    [type] => application/pdf
    [error] => 0
    [size] => 367185
    [tmp_name] => joker
)
Array
(
    [0] => Array
        (
            [name] => order-confirmation-60dc3f6f8d7f3.pdf
            [type] => application/pdf
            [tmp_name] => C:\Users\kbged\Desktop\XAMPP\tmp\php2FDB.tmp
            [error] => 0
            [size] => 367185
        )

)

Any ideas as to why I am unable to update the array value outside the scope of this for loop?

Thanks!

kg__
  • 69
  • 1
  • 6

1 Answers1

2

You have to change the value of the array variable itself

print_r($target_file);
foreach ($target_file as $k => $v) {
   $target_file[$k]['tmp_name'] = "joker";
}
print_r($target_file);
Samir Selia
  • 7,007
  • 2
  • 11
  • 30