How can the code be optimized to accurately store the positions of ones in each column of the array?

The code can be optimized by using a more efficient way to store the positions of ones in each column of the array. One way to do this is by using a multidimensional array where each column index corresponds to an array of positions where ones are located in that column. This allows for easy access and retrieval of the positions of ones in each column.

<?php
// Initialize the array
$array = [
    [0, 1, 0],
    [1, 0, 1],
    [1, 1, 0]
];

// Initialize an empty array to store positions of ones in each column
$ones_positions = [];

// Loop through each column
for ($col = 0; $col < count($array[0]); $col++) {
    $ones_positions[$col] = [];
    
    // Loop through each row in the column
    for ($row = 0; $row < count($array); $row++) {
        if ($array[$row][$col] == 1) {
            $ones_positions[$col][] = $row;
        }
    }
}

// Print the positions of ones in each column
print_r($ones_positions);
?>