1

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

4
  • $colCount !== $cCount Commented Jul 14, 2015 at 7:22
  • define $colCount = array(); above for loop . Commented Jul 14, 2015 at 7:22
  • 1
    What is $colCount within array_push($colCount,$minRow); Commented Jul 14, 2015 at 7:22
  • 1
    ^ i thinl it must be else { array_push($cCount,$minRow); } Commented Jul 14, 2015 at 7:28

3 Answers 3

1

Initialize the $colCount as array() before for loop

$colCount=array();
Sign up to request clarification or add additional context in comments.

Comments

0

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

Read manual

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.