diff --git a/reference/array/functions/array-column.xml b/reference/array/functions/array-column.xml index d0a07fef18..1f58f153ab 100644 --- a/reference/array/functions/array-column.xml +++ b/reference/array/functions/array-column.xml @@ -9,17 +9,17 @@ &reftitle.description; arrayarray_column - arrayarray + arrayinput mixedcolumn_key mixedindex_keynull array_column returns the values from a single column of - the array, identified by the - column_key. Optionally, you may provide an - index_key to index the values in the returned array by - the values from the index_key column in the input - array. + the input, identified by the + column_key. Optionally, an + index_key may be provided to index the values in the + returned array by the values from the index_key + column of the input array. @@ -27,11 +27,15 @@ - array + input - A multi-dimensional array (record set) from which to pull a column of - values. + A multi-dimensional array or an array of objects from which to pull a + column of values from. If an array of objects is provided, then public + properties can be directly pulled. In order for protected or private + properties to be pulled, the class must implement both the + __get and __isset magic + methods. @@ -39,11 +43,11 @@ column_key - The column of values to return. This value may be the integer key of the - column you wish to retrieve, or it may be the string key name for an - associative array. It may also be &null; to return complete arrays - (useful together with index_key to reindex the - array). + The column of values to return. This value may be an integer key of the + column you wish to retrieve, or it may be a string key name for an + associative array or property name. It may also be &null; to return + complete arrays or objects (this is useful together with + index_key to reindex the array). @@ -65,11 +69,35 @@ Returns an array of values representing a single column from the input array. + + &reftitle.changelog; + + + + + + &Version; + &Description; + + + + + 7.0.0 + + Added the ability for the input parameter to be + an array of objects. + + + + + + + &reftitle.examples; - Get column of first names from recordset + Get the column of first names from a recordset - Get column of last names from recordset, indexed by the "id" column + Get the column of last names from a recordset, indexed by the "id" column + + + + Get the column of usernames from the public "username" property of an + object + + +username = $username; + } +} + +$users = [ + new User('user 1'), + new User('user 2'), + new User('user 3'), +]; + +print_r(array_column($users, 'username')); +?> +]]> + + &example.outputs; + + user 1 + [1] => user 2 + [2] => user 3 +) +]]> + + + + + + + Get the column of names from the private "name" property of an object + using the magic <function>__get</function> method. + + +name = $name; + } + + public function __get($prop) + { + return $this->$prop; + } + + public function __isset($prop) : bool + { + return isset($this->$prop); + } +} + +$people = [ + new Person('Fred'), + new Person('Jane'), + new Person('John'), +]; + +print_r(array_column($people, 'name')); +?> +]]> + + &example.outputs; + + Fred + [1] => Jane + [2] => John +) +]]> + + + If __isset is not provided, then an empty array will be + returned. +