Php push object to array
I'm using PHP. I have an array of objects, and would like to add an object to the end of it.
The above is functional, but is there a cleaner and more-readable way to write that? Maybe one line? asked Jan 28, 2013 at 22:21
3 Just do:
You need to create the object first (the You can also do this:
However I would argue that's not as readable, even if it is more succinct. answered Jan 28, 2013 at 22:34
halferhalfer 19.6k17 gold badges92 silver badges175 bronze badges 4 Here is a clean method I've discovered:
answered Feb 9, 2019 at 6:49
ahinkleahinkle 1,8193 gold badges28 silver badges49 bronze badges Do you really need an object? What about:
Just use a two-dimensional array. Output (var_dump):
You could access your last entry like this:
halfer 19.6k17 gold badges92 silver badges175 bronze badges answered Jan 28, 2013 at 22:43
Frederik KammerFrederik Kammer 3,0073 gold badges27 silver badges29 bronze badges 2 Something like:
I think that there are situations where this constructions are preferable to arrays. You can move all the checking logic to the class. Here, before the call to array_filter $myArray has 3 elements. Two correct objects and a NULL. After the call, only the 2 correct elements persist. answered Jan 28, 2013 at 22:56
5 (PHP 4, PHP 5, PHP 7, PHP 8) array_push — Push one or more elements onto the end of array Descriptionarray_push(array repeated for each passed value.
Parametersarray The input array. values The values to push onto the end of the Return ValuesReturns the new number of elements in the array. Changelog
ExamplesExample #1 array_push() example
The above example will output: Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry ) See Also
Rodrigo de Aquino ¶ 10 years ago
bxi at apparoat dot nl ¶ 14 years ago
mrgreen dot webpost at gmail dot com ¶ 6 years ago
willdemaine at gmail dot com ¶ 14 years ago
timer() raat1979 at gmail dot com ¶ 6 years ago
"\npush 1 returns ".array_push($pr,1)."\n"; Carlos Alberto B. Carucce ¶ 3 years ago
egingell at sisna dot com ¶ 16 years ago
= array('a', 'b', 'c'); yhusky at qq dot com ¶ 4 years ago
aosojnik at gmail dot com ¶ 12 years ago
David Spector ¶ 1 year ago
P.A.Semi ¶ 3 years ago
flobee ¶ 8 years ago
helpmepro1 at gmail dot com ¶ 13 years ago
gfuente at garrahan dot gov dot ar ¶ 5 years ago
steve at webthoughts d\ot ca ¶ 16 years ago
andrew at cgipro dot com ¶ 17 years ago
golddragon007 ¶ 7 years ago
aaron dot hawley at uvm dot edu ¶ 17 years ago
siqueiramoises14 at gmail dot com ¶ 2 years ago
bk at quicknet dot nl ¶ 17 years ago
asma dot gi dot 14 at gmail dot com ¶ 10 months ago
wesleys at opperschaap dot net ¶ 13 years ago
is_array($arr)) { alexander dot williamson at gmail dot com ¶ 14 years ago
zbde00 at hotmail dot com ¶ 15 years ago
yuri ¶ 10 years ago
Marc Bernet ¶ 16 years ago
Phil Davies ¶ 17 years ago
oneill at c dot dk ¶ 17 years ago
Chicna ¶ 10 years ago
john ¶ 16 years ago
colecooper2005 at icloud dot com ¶ 5 years ago
rarioj at gmail dot com ¶ 12 years ago
kamprettos at yahoo dot com Teguh Iskanto ¶ 16 years ago
ciprian dot amariei at gmail com ¶ 17 years ago
David Spector ¶ 1 year ago
bart at framers dot nl ¶ 20 years ago
|