How to fix Warning: sizeof(): Parameter must be an array or an object

Can anybody help me fix the error Warning: sizeof(): Parameter must be an array or an object that implements Countable using PHP 7.2 line that gives me an array is $inside_color_count = sizeof( $response->exteriorColor[ $i ]->genericColor );

[php]$color = array();
$colour_count = sizeof( $response->exteriorColor );

$i = 0;
while (!empty($colour_count) && $colour_count > $i ) {
$inside_color_count = sizeof( $response->exteriorColor[ $i ]->genericColor );

if ( ( !in_array( $response->exteriorColor[ $i ]->genericColor->name, $color, true ) ) &&
	( $inside_color_count < 2 ) )
	array_push( $color, $response->exteriorColor[ $i ]->genericColor->name );

if ( $inside_color_count > 1 ) {
	$j = 0;
	while ( $j < $inside_color_count ) {
		if ( ( !in_array( $response->exteriorColor[ $i ]->genericColor->name, $color, true ) ) )
			array_push( $color, $response->exteriorColor[ $i ]->genericColor[ $j ]->name );
		$j++;
	}
}
$i++;

}[/php]

[php]$response->exteriorColor[ $i ]->genericColor[/php]

That is a specific element. What are you actually trying to figure out?

The error I get is this Warning: sizeof(): Parameter must be an array or an object that implements Countable and the error points to the element i mentioned

What are you actually trying to do?

sizeof doesn’t work because you are trying to get the size of an element, not a object or array. So what are you trying to get the count of?

pulling data from a webservice that im converting to xml and trying to get all the objects in an array. it works fine, its pulling the data but for some reason it keeps showing that error

When you look at the XML, does the generic color have multiple nodes?

Try another check,

[php]
$genericColor = $response->exteriorColor[ $i ]->genericColor;
$inside_color_count = is_array($genericColor)?sizeof($genericColor):1;
[/php]

yes it does and if i use a foreach function instead, everything works fine as well but i have to use multiple if else functions to different scenarios but trying to avoid that

Thanks the code you provided worked, the error stops showing

Sponsor our Newsletter | Privacy Policy | Terms of Service