underscore-php
underscore-php copied to clipboard
plucking object properties
I have a common data structure that looks like:
[
{"id":"object1", "name": "foo"},
{"id":"object2", "name": "bar"},
{"id":"object3", "name": "baz"}
]
I would have thought that by typing:
echo json_encode( Arrays::pluck($myArray,'name') );
I would get a response of ["foo","bar","baz"]. The result does seem to work in a simple scenario but if I precede this call with a call to Arrays::find($myArray, function () {...}) then the results of the subsequent pluck() produce inconsistent results.
I don't fully understand this inconsistency yet -- although I'm guessing it may come from a false assumption about where the array pointer is starting -- in any event and for what it's worth here is the resultset I get from pluck after the find operation:
["bar","bar","baz"]
I'm curious. If you stick a call to reset($myArray) between the call to Arrays":find and Arrays::pluck, does it work again?