Giter VIP home page Giter VIP logo

Comments (8)

ramsey avatar ramsey commented on August 27, 2024 1

array_column here and in the PHP core does not recurse into arrays, so this won't work. That could get problematic and very non-performant for deeply-nested arrays.

I would recommend using something like:

$ids = array_column($data['data'], 'id');

to get the column of IDs.

from array_column.

ckmaresca avatar ckmaresca commented on August 27, 2024

Ah, OK. There is no mention of that in the readme, probably should add it.

Unfortunately, your solution doesn't really work when dealing with large & variable amounts of inbound data, so I'll have to revert back to our existing code (which recurses but is less clean).

It's too bad as searching for a specific repetitive key and getting all the values is a very, very common task when dealing with APIs. Pretty much every REST/JSON API has these data patterns....

from array_column.

ramsey avatar ramsey commented on August 27, 2024

The function is intended mainly for use with data structures like database recordsets or CSV data. I'm open to suggestions on how it should work with different types of array structures, though. I'll just need to make sure the same functionality is ported into PHP core and that the function definition remains relatively unchanged (for BC).

from array_column.

ckmaresca avatar ckmaresca commented on August 27, 2024

@ramsey Thanks for the note - here is some code I just dug up (untested but will try it shortly) that seems to do a recursive array_column:

https://gist.github.com/tripflex/2818993b85db39a1f89a
Note: after looking at this code, I'd much rather see it implemented using the Recursive Iterator - there are a number of StackExchange posts referencing this.... That said, it probably works as is, even if it's not the most elegant implementation...

IMHO this should really, really be in core PHP, it would make working with APIs vastly easier. As a long-time PHP user (e.g. since PHP/FI 1.0) and someone who deals with a LOT of APIs (300+) this would make my life a lot simpler while avoiding the need to maintain even more custom code.

from array_column.

ckmaresca avatar ckmaresca commented on August 27, 2024

Tested the above Gist, works well, returns an array of values as expected, at least when given the array I posted above.

from array_column.

ramsey avatar ramsey commented on August 27, 2024

Thanks. I'm going to reopen this issue and mark it as an enhancement. I've got several in my queue to enhance array_column, and I hope to find some time in the near future to devote to it.

from array_column.

ckmaresca avatar ckmaresca commented on August 27, 2024

Cool! Thanks for that. I'm not sure I have time to fork & implement, but I'll try to see if I can at least find some code that uses the Recursive Iterator.

from array_column.

gsi-jorge avatar gsi-jorge commented on August 27, 2024

I use the code posted on https://gist.github.com/tripflex/2818993b85db39a1f89a
and work very well.
I think that this method need to be added to PHP's core too.

from array_column.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.