-1

I am populating an array $du through a foreach loop like this

foreach ( $arr as $v => $r ) {
            $du['id'] = $r['id'];
            $du['name'] = $r['name'];
            $Sales->insert($du);
            $du = '';
   }

The above piece of code is populating $du properly in PHP 5.6 and PHP 7.0.2 but when we upgraded to PHP 7.2 $du is returning 1.

abbas
  • 238
  • 2
  • 18
  • 4
    Did you define the array in advance? `$du = array();`? Also, why do you have `$du = '';`? You reset `$du` on every iteration. – Jay Blanchard Aug 06 '19 at 12:17
  • _“$du is returning 1”_ - what do you even mean by that? There is no return statement anywhere in the code you have shown. Are you actually talking about the return value of the method call `$Sales->insert($du);`, or what? – misorude Aug 06 '19 at 12:19
  • What do you mean with **return 1**? Is this code in a function or method? Where does it get called? Please post the full code. – Daan Aug 06 '19 at 12:20
  • By returning I mean when I `print_r` `$du` it returns 1. – abbas Aug 06 '19 at 12:21
  • @misorude yes I mean return value of the method call. – abbas Aug 06 '19 at 12:21

1 Answers1

0

As per the comment resetting $du works.

foreach ( $arr as $v => $r ) {
            $du['id'] = $r['id'];
            $du['name'] = $r['name'];
            $Sales->insert($du);
            $du = [];
   }
abbas
  • 238
  • 2
  • 18