underscore-php icon indicating copy to clipboard operation
underscore-php copied to clipboard

plucking object properties

Open yankeeinlondon opened this issue 11 years ago • 1 comments

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"]

yankeeinlondon avatar May 21 '14 13:05 yankeeinlondon

I'm curious. If you stick a call to reset($myArray) between the call to Arrays":find and Arrays::pluck, does it work again?

mcordingley avatar May 16 '15 22:05 mcordingley