Get value from array of objects php
The solution depends on the PHP version you are using. At least there are 2 solutions: Show First (Newer PHP versions)As @JosepAlsina said before the best and also shortest solution is to use
Notice: For iterating an Second (Older PHP versions)@Greg said in older PHP versions it is possible to do following:
But beware: In newer PHP versions
The differenceFirst solution creates a new function and puts it into your RAM. The garbage collector does not delete the already created and already called function instance out of memory for some reason. And that regardless of the fact, that the created function instance can never be called again, because we have no pointer for it. And the next time when this code is called, the same function will be created again. This behavior slowly fills your memory... Both examples with memory output to compare them: BAD
GOOD
This may also be discussed hereMemory leak?! Is Garbage Collector doing right when using 'create_function' within 'array_map'? (PHP 5 >= 5.5.0, PHP 7, PHP 8) array_column — Return the values from a single column in the input array Descriptionarray_column(array Parametersarray 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. column_key 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 index_key
The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name. The value is cast as usual for array keys (however, prior to PHP 8.0.0, objects supporting conversion to string were also allowed). Return ValuesReturns an array of values representing a single column from the input array. Changelog
ExamplesExample #1 Get the column of first names from a recordset
The above example will output: Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter ) Example #2 Get the column of last names from a recordset, indexed by the "id" column
The above example will output: Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe ) Example #3 Get the column of usernames from the public "username" property of an object
__construct(string $username) The above example will output: Array ( [0] => user 1 [1] => user 2 [2] => user 3 ) Example #4 Get the column of names from the private "name" property of an object using the magic __get() method.
__construct(string $name) The above example will output: Array ( [0] => Fred [1] => Jane [2] => John ) If __isset() is not provided, then an empty array will be returned. mohanrajnr at gmail dot com ¶ 7 years ago
WARrior ¶ 9 years ago
balbuf ¶ 4 years ago
ff2 AT hotmail DOT co DOT uk ¶ 4 years ago
$indexkey === null) { $result[] = $val; } yangmeishu at live dot com ¶ 2 years ago
= [ till at etill dot net ¶ 7 years ago
nino at recgr dot com ¶ 6 years ago
miguelfzarth at gmail dot com ¶ 6 years ago
antonfedonjuk at gmail dot com ¶ 7 years ago
Anonymous ¶ 6 years ago
$index_key !== null) { Carlos Granados ¶ 6 years ago
array_intersect_key($data, $keep); Hiranmoy Chatterjee ¶ 6 months ago
benjam ¶ 6 years ago
= array( oleg dot bolden at gmail dot com ¶ 4 months ago
$output; Sbastien ¶ 5 months ago
1184427175 at qq dot com ¶ 5 years ago
Nolan chou ¶ 6 years ago
kaspar dot wilbuer at web dot de ¶ 6 years ago
katrinaelaine6 at gmail dot com ¶ 5 years ago
= [ kiler129 @ nowhere ¶ 7 years ago
hypxm at qq dot com ¶ 7 years ago
marianbucur17 at yahoo dot com ¶ 7 years ago
robbieaverill[at]gmail.com ¶ 7 years ago
myles at smyl dot es ¶ 7 years ago
greensea ¶ 6 years ago
$index_key === NULL || !isset($v[$index_key])) { coviex ¶ 7 years ago
robsonvnasc at gmail dot com ¶ 5 years ago
Dominik59 ¶ 5 years ago
How get values from array of objects in PHP?Method 1: Using json_decode and json_encode method: The json_decode function accepts JSON encoded string and converts it into a PHP variable on the other hand json_encode returns a JSON encoded string for a given value. Syntax: $myArray = json_decode(json_encode($object), true);
How can we get particular data from array in PHP?Answer: Use the Array Key or Index
If you want to access an individual value form an indexed, associative or multidimensional array you can either do it through using the array index or key.
How do you call an array of objects in PHP?Array elements can be accessed using the array[key] syntax. ); var_dump($array["foo"]); var_dump($array[42]);
How do you access values in an array?To access an individual element of an array, use the name of the array name followed by the index of the element in square brackets. Array indices start at 0 and end at size-1: array_name[index]; accesses the index'th element of array_name starting at zero.
|