array_column icon indicating copy to clipboard operation
array_column copied to clipboard

Not working on deeper arrays?

Open ckmaresca opened this issue 10 years ago • 8 comments

I'm trying to parse the following array **:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 78987967867
                    [created_time] => 2013-06-16T02:30:15+0000
                )

            [1] => Array
                (
                    [id] => 45645645645
                    [created_time] => 2013-04-20T17:43:46+0000
                )

            [2] => Array
                (
                    [id] => 5675676756756
                    [created_time] => 2013-04-08T20:52:57+0000
                )

            [3] => Array
                (
                    [id] => 5767567657
                    [created_time] => 2013-02-15T23:08:10+0000
                )

            [4] => Array
                (
                    [id] => 6556765756
                    [created_time] => 2013-02-15T23:08:02+0000
                )

            [5] => Array
                (
                    [id] => 345345345
                    [created_time] => 2010-01-25T06:44:05+0000
                )

            [6] => Array
                (
                    [id] => 345345656
                    [created_time] => 2008-12-06T02:12:25+0000
                )

            [7] => Array
                (
                    [id] => 2453453453
                    [created_time] => 2008-02-16T02:36:45+0000
                )

            [8] => Array
                (
                    [id] => 1232434234
                    [created_time] => 2007-07-08T01:56:34+0000
                )

            [9] => Array
                (
                    [id] => 6512005646
                    [created_time] => 2007-07-08T01:55:14+0000
                )

            [10] => Array
                (
                    [id] => 6512010646
                    [created_time] => 2007-03-13T20:44:19+0000
                )

            [11] => Array
                (
                    [id] => 6512015646
                    [created_time] => 2007-02-16T15:12:42+0000
                )

        )

    [paging] => Array
        (
            [cursors] => Array
                (
                    [before] => sdkj898hnnksdffsdf=
                    [after] => moiusdujnekwero==
                )

        )
)

This array is contained in $data. Calling array_column($data,'id') returns an empty array:

Array
(
)

There are no errors, no warnings, no notices. Unless I'm doing something wrong, it would seem that there is some sort of limit on array depth?

Ideas? Particularly how to debug this?

** this is a standard datastructure from the Facebook API, turn into a PHP array by using json_decode($json, TRUE)

ckmaresca avatar Mar 20 '15 16:03 ckmaresca

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.

ramsey avatar Mar 20 '15 16:03 ramsey

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....

ckmaresca avatar Mar 20 '15 16:03 ckmaresca

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).

ramsey avatar Mar 20 '15 16:03 ramsey

@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.

ckmaresca avatar Mar 20 '15 16:03 ckmaresca

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

ckmaresca avatar Mar 20 '15 17:03 ckmaresca

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.

ramsey avatar Mar 20 '15 17:03 ramsey

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.

ckmaresca avatar Mar 20 '15 21:03 ckmaresca

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.

gsi-jorge avatar May 02 '16 21:05 gsi-jorge