No Code Attached Yet bug
avatar Ruud68
Ruud68
20 Dec 2022

Steps to reproduce the issue

create array:

        $items = [];
        $items[] = ['column_a' => 1, 'column_b' => 2, 'column_c' => 3];
        $items[] = ['column_a' => 4, 'column_b' => null, 'column_c' => 6];

use ArrayHelper::dropColumn to drop column_b from the array

var_dump(ArrayHelper::dropColumn($items, 'column_b'));

Expected result

column column_b removed from array

array(2) {
  [0]=>
  array(2) {
    ["column_a"]=>
    int(1)
    ["column_c"]=>
    int(3)
  }
  [1]=>
  array(3) {
    ["column_a"]=>
    int(4)
    ["column_c"]=>
    int(6)
  }
}

Actual result

column column_b partially removed from array resulting in unexpected behavior / result

array(2) {
  [0]=>
  array(2) {
    ["column_a"]=>
    int(1)
    ["column_c"]=>
    int(3)
  }
  [1]=>
  array(3) {
    ["column_a"]=>
    int(4)
    ["column_b"]=>
    NULL
    ["column_c"]=>
    int(6)
  }
}

System information (as much as possible)

Joomla 4.2.6 but probably already from version 1.5 (as that is when this function was added)

Additional comments

Issue is caused both for arrays and object. The function uses isset() to determine if a key is set in the object / array and if set remove that. But isset() also checks the value, when that is null (like in the example given) then it will also return false > resulting in the key not being unset in the function.

for arrays array_key_exist() and for object property_exists() should be used to determine if key is set.

avatar Ruud68 Ruud68 - open - 20 Dec 2022
avatar Ruud68 Ruud68 - change - 20 Dec 2022
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 20 Dec 2022
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 20 Dec 2022
avatar joomdonation
joomdonation - comment - 22 Dec 2022

@Ruud68 Agree. Could you please make PR with your proposed solution?

avatar chmst chmst - change - 27 Dec 2022
Labels Added: bug
avatar chmst chmst - labeled - 27 Dec 2022
avatar Kaushik1216
Kaushik1216 - comment - 27 Dec 2022

Is this correct changes in class ArrayHelper dropcolumn function
For object
Replacing : if (\is_object($item) && isset($item->$colName))
With : if (\is_object($item) && property_exists($item,$colName))

For Array
Replacing :elseif (\is_array($item) && isset($item[$colName]))
With : elseif (\is_array($item) && (array_key_exists($colName,$item))

If yes I will make pr for same

avatar chmst
chmst - comment - 27 Dec 2022

You could make a PR with your suggestion, then people can test or review and suggest changes.

avatar Hackwar Hackwar - change - 25 Aug 2023
Labels Added: PBF
avatar Hackwar Hackwar - labeled - 25 Aug 2023
avatar brianteeman brianteeman - change - 1 Sep 2023
Labels Removed: PBF
avatar brianteeman brianteeman - unlabeled - 1 Sep 2023

Add a Comment

Login with GitHub to post a comment