Link to home
Start Free TrialLog in
Avatar of jblayney
jblayneyFlag for Canada

asked on

fix PHP array_filter warning :array_filter() expects parameter 1 to be array, boolean given

Hello,

I am trying to fix a PHP warning I am getting. The page works, im just trying to clean up errors. Sorry about formatting, but EE is forcing everything to be in code

the error is 

array_filter() expects parameter 1 to be array, boolean given

the line that is throwing the error is

 $isMulti = !empty(array_filter($attributes, function ($e) {
            return is_array($e);
        }));


This is the code.

    public function arrayToAttributesHTML($attributes, $extra_attributes = [])
    {
        if (null === $attributes) {
            return '';
        }
        if (empty($attributes) && empty($extra_attributes)) {
            return '';
        }
       
        $isMulti = !empty(array_filter($attributes, function ($e) {
            return is_array($e);
        }));
       
        if ($isMulti) {
            foreach ($attributes as $key => $values) {
                $attributes[$values['name']] = $values['value'];
                unset($attributes[$key]);
            }
        }
        /**
         * Add or merged attribute
         */
        if (!empty($extra_attributes)) {
            foreach ($extra_attributes as $attribute_name => $attribute_value) {
                if (isset($attributes[$attribute_name])) {
                    $attribute_value = $attributes[$attribute_name] . ' ' . $attribute_value;
                }
                $attributes[$attribute_name] = $attribute_value;
            }
        }
        /**
         * Convert array to string html
         */
        $attribute_string = '';
        foreach ($attributes as $key => $value) {
            $attribute_string .= $key . '="' . htmlspecialchars($value) . '" ';
        }
        return $attribute_string;
    }
}

Avatar of David Favor
David Favor
Flag of United States of America image

The fix is... to pass an array...

Use https://www.php.net/manual/en/function.is-array.php then bail out as an error if a non-array is passed.

Something like this to match the rest of your code...

if (!is_array($attributes)) {
   return '';
}

Open in new window

Avatar of jblayney

ASKER

thank you
Hello Dave,

What you posted in absolutely correct.. and I'm happy to give points.. The problem is that if I bail, my function completely dies.. but it was working with the warning.

Could we possibly convert it to an array, something like this ?

if (!is_array($attributes)) {
   convert $attributes to array
}

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem is the $attributes parameter you are passing to the function. The warning is telling you this is a boolean and not an array.

Dave's solution is a catch for bad data - it is not a fix for your problem.

You need to go to where you call arrayToAttributesHTML( ) and look at how you are compiling the $attributes parameter - something is not working there.

If you need help with this you need to post that code as well (please use code blocks if you do)