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'));
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)
}
}
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)
}
}
Joomla 4.2.6 but probably already from version 1.5 (as that is when this function was added)
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.
Labels |
Removed:
?
|
Labels |
Added:
No Code Attached Yet
|
Labels |
Added:
bug
|
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
You could make a PR with your suggestion, then people can test or review and suggest changes.
Labels |
Added:
PBF
|
Labels |
Removed:
PBF
|
@Ruud68 Agree. Could you please make PR with your proposed solution?