-1

I've just upgraded to PHP 7.2 and I'm getting PHP Fatal error: Uncaught Error: Cannot use string offset as an array for the line $myarray[$ex][0] = $oldarray[$z]['ID']; in this old code...

                $ex=0;
                $z=0;
                while($oldarray[$z]['ID']){
                    if(0==$duplicatename or !$duplicatename){
                        $ex++;
                        $myarray[$ex][0] = $oldarray[$z]['ID'];
                        /*...*/
                    }
                    $z++;
                }

I've read that the more recent versions of PHP have backward incompatible changes so I changed the offending line to $myarray[$ex]['ID'] = $oldarray[$z]['ID']; but I'm getting the same error.

The site this code is from is old so I was really expecting more problems than just this one piece of code. Can anyone tell me what I'm doing wrong here please.

Here is a var_dump of $oldarray, I've edited it slightly to change the content...

array(4) { [0]=> array(11) { ["ID"]=> int(59480) ["boxid"]=> int(11158) ["galleryurl"]=> string(75) "http://..." ["gallerytitle"]=> string(63) "Gallery title goes here" ["gallerytype"]=> string(5) "photo" ["gallerydate"]=> int(1542544634) ["galleryactive"]=> string(1) "y" ["usersubmitted"]=> string(1) "n" ["userip"]=> NULL ["galleryhot"]=> string(1) "n" ["gallidentify"]=> int(372239531175) } 1=> array(11) { ["ID"]=> int(59324) ["boxid"]=> int(11158) ["galleryurl"]=> string(79) "http://..." ["gallerytitle"]=> string(45) "Gallery title goes here" ["gallerytype"]=> string(5) "photo" ["gallerydate"]=> int(1538477012) ["galleryactive"]=> string(1) "y" ["usersubmitted"]=> string(1) "n" ["userip"]=> NULL ["galleryhot"]=> string(1) "n" ["gallidentify"]=> int(428341079414) } }

sdexp
  • 756
  • 4
  • 18
  • 36
  • 1
    What does `$oldarray` look like? What does `$myarray` look like? – miken32 Dec 07 '18 at 22:40
  • You can do an is_array() check to see if the elements are indeed arrays – Paul Jerome Bordallo Dec 07 '18 at 22:44
  • @miken32 It's old code and it's not the best but $oldarray[integer]['string'] and $myarray is now an identical format to $oldarray. – sdexp Dec 07 '18 at 22:45
  • `var_dump` it and edit your question to include that info. See more on a [mcve]. – miken32 Dec 07 '18 at 22:46
  • Also your error message will tell you exactly which line is causing this problem. – miken32 Dec 07 '18 at 22:50
  • The exact line is the one I stated in the question `$myarray[$ex][0] = $oldarray[$z]['ID'];` which I changed to `$myarray[$ex]['ID'] = $oldarray[$z]['ID'];`. Which means that $myarray isn't getting created at all. – sdexp Dec 07 '18 at 23:08

1 Answers1

1

Is there a possibility to refactor it with using of foreach?

http://php.net/manual/en/control-structures.foreach.php

What you will get if you will use:

print_r($oldarray);
print_r($myarray);

?

There is some probability you have object in second array dimension ... we need to see a bigger piece of code to help you effectively ...

What will happen if you will change:

$myarray[$ex][0] = $oldarray[$z]['ID'];

by this way:

$myarray[$ex][0] = $oldarray[$z].ID;

?

VitezslavSimon
  • 342
  • 2
  • 7
  • 1
    Try add: def $myarray = array(); on start of your code piece to ensure its declared in suitable way. – VitezslavSimon Dec 07 '18 at 23:13
  • 1
    Welcome to Stackoverflow and thanks. I think using a `foreach` is probably the way to go. I'll play around with that now. – sdexp Dec 07 '18 at 23:15
  • 1
    What will do similar way like this example: http://php.net/manual/en/function.array-push.php#83388 ? Thanks for voting me up :) – VitezslavSimon Dec 07 '18 at 23:17