What is the best method to fill a multidimensional array in PHP?
When filling a multidimensional array in PHP, the best method is to use nested loops to iterate over the array dimensions and assign values to each element. This allows for precise control over the placement of data within the array.
// Initialize an empty multidimensional array
$multiArray = [];
// Define the dimensions of the array
$rows = 3;
$columns = 3;
// Fill the multidimensional array with values
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $columns; $j++) {
$multiArray[$i][$j] = $i * $j;
}
}
// Print the multidimensional array
print_r($multiArray);