1
votes

This is my code

$items = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
    $numCols = 4;
    $result = ArrayVals($items,$numCols);
    echo $result;
    function ArrayVals($items,$numCols) {
        $minRow = floor(count($items)/$numCols);
        $remaining = count($items) % $numCols;
        $cCount = array();
        for ($i = 0;$i<$numCols;$i++) {
            if ($i < $remaining) {
                array_push($cCount,$minRow+1);
            } else {
                array_push($colCount,$minRow);
            }
        }
        $listString = '';
        $count = 0;
        for ($i = 0;$i<count($cCount);$i++) {
            $listString = $listString . "<ul>";
            for ($j = 0;$j<$cCount[$i];$j++) {
                $listString = $listString . '<li>' . $items[$count] . '</li>';
                $count = $count + 1;
            }
            $listString = $listString . '</ul>';
        }
        return $listString;

i am getting the

Warning: array_push() expects parameter 1 to be array, null given in C:\UniServer\www\RnD\Test2\t2.php on line 20

how should i solve it

3
$colCount !== $cCount - Mark Baker
define $colCount = array(); above for loop . - Saty
What is $colCount within array_push($colCount,$minRow); - Narendrasingh Sisodia
^ i thinl it must be else { array_push($cCount,$minRow); } - splash58

3 Answers

1
votes

Initialize the $colCount as array() before for loop

$colCount=array();
0
votes

$colCount is undefined . array_push expects first parameter to be an array .

Read manual

0
votes

The variable $colCount not defined in its scope. Initialize the variable $colCount in function ArrayVals().