简体   繁体   中英

how to store values in an empty 2d array in php with 20x20 size 2d array

How can I store values in an empty 2d array? here is my code. Please tell me what is wrong with the code I'm an newbie trying to learn PHP. Thanks!

$array2d = array(array());

How can I store data make it a 20x20 array?

If there's nothing between the lines and all you're asking is how to create an empty 20x20 array, this is the way to do it:

$array2d = array_fill(0, 20, array_fill(0, 20, null));

To break that down, the inner array_fill() creates a one-dimensional array with 20 items, which are all null (replace that with any value you want). The outer array_fill() then creates a one-dimensional array filled with 20 arrays created with the inner function.

To set a value to a given index:

$array2d[5][12] = 'A';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM