|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-04-15 13:46 UTC] mark at lange dot demon dot co dot uk
Description: ------------ PHP7 extended array_column() to support reading a column of property values from an array of objects. However, if the property contains a value that loose-equates to null, that entry is not returned in the resulting array. Applies to all PHP7 releases; http://lxr.php.net/xref/PHP_7_0/ext/standard/array.c#3569 appears to be the cause of the bug The code test script demonstrates this problem. Expected result is an array containing [-1,0,1,2,null,true,false,'abc',''] Actual result is an array containing [-1,1,2,true,'abc'] The 0, null, false and empty string values are all discarded Test script: --------------- class myObj { public $prop; public function __construct($prop) { $this->prop = $prop; } } $objects = [ new myObj(-1), new myObj(0), new myObj(1), new myObj(2), new myObj(null), new myObj(true), new myObj(false), new myObj('abc'), new myObj(''), ]; var_dump(array_column($objects, 'prop')); Expected result: ---------------- array(9) { [0]=> int(-1) [1]=> int(0) [2]=> int(1) [3]=> int(2) [4]=> null [5]=> bool(true) [6]=> bool(false) [7]=> string(3) "abc" [8]=> string(0) "" } [-1,0,1,2,null,true,false,'abc',''] Actual result: -------------- array(5) { [0]=> int(-1) [1]=> int(1) [2]=> int(2) [3]=> bool(true) [4]=> string(3) "abc" } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 22:00:01 2025 UTC |
Unsure if it's related, but array_column() also ignores object properties that are numerics or invalid property names, even though these can exist (for example, when casting to a stdClass object from an array). e.g $objects = [ (object)["3" => 1], (object)["3" => 2], ]; print_r($objects); print_r(array_column($objects, 3)); and $objects = [ (object)["@invalidProperty" => 1], (object)["@invalidProperty" => 2], ]; print_r($objects); print_r(array_column($objects, "@invalidProperty"));