Yii2: bug with ArrayHelper::filter($array, $filters)

Created on 18 Nov 2020  路  6Comments  路  Source: yiisoft/yii2

Hi i found a bug in
public static function filter ($ array, $ filters)
yii2 / framework / helpers / BaseArrayHelper.php file
line 947 if ($ filter [0] === '!')
if $ filters is one dimensional array i see

PHP Notice - yii \ base \ ErrorException

Trying to access array offset on value of type int
1.in /var/www/test_dev/vendor/yiisoft/yii2/helpers/BaseArrayHelper.phpat line 947

 * @return array Filtered array
 * @since 2.0.9
 * /
public static function filter ($ array, $ filters)
{
    $ result = [];
    $ excludeFilters = [];

    foreach ($ filters as $ filter) {
        if ($ filter [0] === '!') {
            $ excludeFilters [] = substr ($ filter, 1);
            continue;
        }

        $ nodeValue = $ array; // set $ array as root node
        $ keys = explode ('.', $ filter);
        foreach ($ keys as $ key) {
            if (! array_key_exists ($ key, $ nodeValue)) {
                continue 2; // Jump to next filter

In older versions it worked like this
$ array:

Array
(
    [0] => #
    [1] => Action Column
    [2] => User
    [3] => Subject
    [4] => Is Active
    [5] => Created At
    [6] => Updated At
)

$ filters:

Array
(
    [0] => 0
    [1] => 1
    [2] => 3
    [3] => 2
    [4] => 6
    [5] => 5
)

$ result:

Array
(
    [0] => #
    [1] => Action Column
    [3] => Subject
    [2] => User
    [6] => Updated At
    [5] => Created At
    [4] => Is Active
)

It was possible to use ArrayHelper :: filter to display the array in the desired order.
I fixed it like this

- 947 if ($ filter [0] === '!') {
+ 947 if (is_array ($ filter) && $ filter [0] === '!') {

Maybe it should be included this in the repository?

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.39.2
| PHP version | 7.4.3
| Operating system | Ubuntu 20.04

bug

Most helpful comment

Hmm, right. The change introduced in https://github.com/yiisoft/yii2/commit/b794d76056ac2fe7426018f5400e9921a4952a55#diff-e6a3dd2628f9c50a496966ef3ba411cae921504c003153c65d723292ed1a3a2c rendered using integer keys here impossible. I'll try to re-enable it.

All 6 comments

@rikcage do you have time for a pull request with a unit test and the fix?

It seems that @rikcage uses integer values in the filters argument, but they are expected to be strings.

I think this is a misunderstanding of the cause of this error. $filters must be a flat array (and it should be checked BTW). The problem here is the same as in #18426 - the check for the first character being exclamation mark is done accessing the string as an array.

I'll fix this together with all other places having similar solution.

@bizley But the notice from bug report says 'Trying to access array offset on value of type int', and the flat array of integers is passed as an argument.

Hmm, right. The change introduced in https://github.com/yiisoft/yii2/commit/b794d76056ac2fe7426018f5400e9921a4952a55#diff-e6a3dd2628f9c50a496966ef3ba411cae921504c003153c65d723292ed1a3a2c rendered using integer keys here impossible. I'll try to re-enable it.

Was this page helpful?
0 / 5 - 0 ratings