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;
    }
}

PHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
David Favor

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

ASKER
jblayney

thank you
ASKER
jblayney

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


All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
David Favor

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Julian Hansen

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)