sizeof(): Parameter must be an array or an object

419
February 21, 2018, at 4:20 PM

I have this error can somebody help me.

This is my code:

function getMenu($parentId = 0)
{
    $sql = "select * from tbl_category where parent=?";
    $result = $this->doSelect($sql, array($parentId));
    foreach ($result as $row) {
        $children = $this->getMenu($row['id']);
       if (sizeof ($children) > 0) {
            $row['children'] = $children;
        }
        @$data[] = $row;
    }
    return @$data;
}

and this is my error

Warning: sizeof(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\digikalamvc\core\model.php on line 302

Answer 1

You need to do below change in your function:-

1.Create an empty array

2.Check for not-empty $children.

3.Assign value to the newly created empty array inside loop.

4.Return the final array

5.remove error-escaping @ from your code.

So code need to be like below:-

function getMenu($parentId = 0){
    $sql = "select * from tbl_category where parent=?";
    $result = $this->doSelect($sql, array($parentId));
    $final_array = []; //create an empty array
    foreach ($result as $row) {
       $children = $this->getMenu($row['id']);
       if (!empty($children)) { //check for not-empty
            $final_array['children'][] = $children; //assign value to array
        }
    }
    return $final_array; //return final array 
}
Answer 2

Please update the condition such as I have updated following code. I hope it will be helpful for you.

function getMenu($parentId = 0)
{
    $sql = "select * from tbl_category where parent=?";
    $result = $this->doSelect($sql, array($parentId));
    foreach ($result as $row) {
        $children = $this->getMenu($row['id']);
       if (!empty($children) && sizeof ($children) > 0) {
            $row['children'] = $children;
        }
        @$data[] = $row;
    }
    return @$data;
}
Rent Charter Buses Company
READ ALSO
ffmpeg concat two video and add watermark after concating video give me blank mp4 file on server but in local working perfectly why?

ffmpeg concat two video and add watermark after concating video give me blank mp4 file on server but in local working perfectly why?

i have two video i want to concat them and then add watermark to them but after command run give me blank mp4 file on server but in local working perfectly and on server two video concat perfectly but after that code is not running

204
Passing array object in SOAP call using PHP

Passing array object in SOAP call using PHP

Given the below SOAP function (login) and types, how can I send (i need syntax) a SOAP request using php? I'm using WSDL file

230
Laravel 5.5 Error when trying to fetch data from API for Client Side

Laravel 5.5 Error when trying to fetch data from API for Client Side

I have problem with my code, I can't fetch data from an API to display categories in client side the message "Trying to get property of non-object"

189
How to validate image file before uploading in php?

How to validate image file before uploading in php?

I am trying to upload a file where i want to allow only png filesFor validation, i would check wether extension of file is

209