Multidimensional array problem

Hi guys,
i have one problem with multidimensional array.
I fill the field in multiple files (6-7 of them) like this:
$data[‘zone’.$zone][$code_row[‘position’]][] = $code_row[‘code’];

first dimension can be zone1, zone2, zone3…
second dimension is numbers like 10,11,12,20,25,100… (there can be more separate arrays )
third dimension is html code but i need key 0 for that third dimension

So my array should look like:

This code works fine on PHP 7.0 but now I need to update it on PHP 7.3 and its not working.

Error i get is:

Uncaught Error: Cannot use string offset as an array in /var/www/clients/client1/web35/web/inc/print_menu.php:651 Stack trace: #0 /var/www/clients/client1/web35/web/inc/get_content.php(54): include() #1 /var/www/clients/client1/web35/web/index.php(151): include(’/var/www/client…’) #2 {main} thrown in /var/www/clients/client1/web35/web/inc/print_menu.php on line 651

Anyone can help?

I would say this is your issue,

Strict Interpretation of Integer String Keys on ArrayAccess
Array accesses of type $obj[“123”] , where $obj implements ArrayAccess and “123” is an integer string literal will no longer result in an implicit conversion to integer, i.e., $obj->offsetGet(“123”) will be called instead of $obj->offsetGet(123) . This matches existing behavior for non-literals. The behavior of arrays is not affected in any way, they continue to implicitly convert integeral string keys to integers.

But i would need to see more code to understand how to fix it.

I will try to explain. I need array like this one:

I have main file where i call 5-6 other files and in them i fill array (same array in all files).
In these files I do a query to the database and through while loop I declare certain variables and filling array.
PHP 7.0 works fine and code is:
$data[‘zone’.$code_row[‘zone’]][$code_row[‘position’]][] = $html;
I have variable $zone from database (just adding word “zone” on begin), same as position.
And i have variable $html where I put HTML I need (example $html = “< article >< h1 >’.$code_row[‘title’].’< /h1 >< /article >”; )

But same code doesnt work on PHP 7.3 and thats what I’m tryng to figure out.
It seems there is different way how I should fill that array.

Have you tried array_push?

array_push($data[‘zone’.$code_row[‘zone’]][$code_row[‘position’]], html)

array_push doesnt make new dimension. Just pushing element on the end of the array.

that is exactly what this is doing,

$data[‘zone’.$code_row[‘zone’]][$code_row[‘position’]][] = $html

I tried now on both version PHP, on 7.3 im getting same error as with code before.
And on version 7.0 also error " Warning : array_push() expects parameter 1 to be array, null given in path on line 652"

I declared empty array in main file ( $data = array():wink: and $code_row[‘zone’], $code_row[‘position’] and $html have values so I’m not sure why I’m getting this error on 7.0 version.

But this version is not problem, on 7.3 i get same error as with code I copied before.

Is $data already declared as an array outside of your loop?

This sounds like an XY Problem. Tell us about the real problem you are trying to solve with all this code instead of asking about your attempted solution to it. Your description has a strong code smell to it. Something is not right.

yes $data is declared in main file where i call others files and filling that array in them.

This is code from custom made CMS.
What I am trying to do is the following:
I have file get_data.php where where I call oher files (print_menu.php, print_content.php, print_module.php…).
In these files I have database query’s where I pull data from the database (from different tables - depenting on the file) and save data as HTML in this array.
Then in the main file i do the actual print on the web page.

I used PHP 7.0 until now, and now I’m trying to update code to 7.3…everything works fine except this line of code.

I solved problem. Not sure why this solves the problem, but it worked.
I declare array for each file ($data_module, $data_content, $data_menu…) and fill each array with data like before (didn’t change old code from above) and in main file i used function array_replace_recursive like this:
$data = array_replace_recursive($data_menu, $data_modules, $data_content, $data_gallery, $data_downloads, $data_code, $data_video);

And it worked.
Thanks guys.

Sponsor our Newsletter | Privacy Policy | Terms of Service