Anahkiasen / underscore-php

A redacted PHP port of Underscore.js with additional functions and goodies – Available for Composer and Laravel
http://anahkiasen.github.com/underscore-php/
1.12k stars 88 forks source link

plucking object properties #34

Open yankeeinlondon opened 10 years ago

yankeeinlondon commented 10 years ago

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"]
mcordingley commented 9 years ago

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