Problem/Motivation

(#)
SearchBuilder::flattenKeys throws Array offset Notice in PHP 7.4
error image
When flattenKeys method is invoked this code

$keys = array_filter($keys, function ($key) {
      return $key[0] !== '#';
    }, ARRAY_FILTER_USE_KEY);

generates the error shown.

Example:
Let's says $keys array looks like :

Array
(
    [0] => jstestsearch
    [#conjunction] => AND
)

key: 0 is considered as int and when we try to access $key[0] we are trying to access value considering it a string but it's recognised as an int hence the error.

Proposed resolution

(#)
Changing it to

$keys = array_filter($keys, function (string $key) {
      return $key[0] !== '#';
    }, ARRAY_FILTER_USE_KEY);

notice the argument hint:string in the function, this seems to work fine and fixes the error.

Comments

u_tiwari created an issue. See original summary.

u_tiwari’s picture

StatusFileSize
new561 bytes

Attaching a patch for the same.

baysaa’s picture

Version: 8.x-7.0-alpha2 » 8.x-7.x-dev
Status: Active » Reviewed & tested by the community
Related issues: +#3157883: Elasticsearch results when using PHP 7.4.7 gives "Trying to access array offset on value of type int"

+1 #2. Simple logical fix.

This is from the the PHP 7.4 migration doc: Trying to use values of type null, bool, int, float or resource as an array (such as $null["key"]) will now generate a notice.

nils.destoop’s picture

+1 Fixes the notice for me

dealancer’s picture

This patch works perfectly fine, let's commit it!

  • skek committed 85faf68 on 8.x-7.x authored by u_tiwari
    Issue #3125891 by u_tiwari: SearchBuilder::flattenKeys throws Array...
skek’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.