0

I have the following array :

Array
(
[0] =Array(
        [date] =2016-09-16
        [data] = Array(
                [0] =Array
                    (
                        [ID] =1945
                        [Debit] =CREDIT
                        [timestamp] =1474025139
                        [LastName] =test1
                        [FirstName] =test1

                    )

                [1] =Array
                    (
                        [ID] =1946
                        [Debit] =CREDIT
                        [timestamp] =1474025140
                        [LastName] =test2
                        [FirstName] =test2
                    )
               [2] =Array
                    (
                        [ID] =1947
                        [Debit] =CREDIT
                        [timestamp] =1474025130
                        [LastName] =test3
                        [FirstName] =test3
                    )
            )
        [start_date] =2016-09-16
        [end_date] =2016-09-16
        [show_fee] =0
    )

[1] =Array
    (
        [date] =2016-09-15
        [data] = Array
            (
                [0] =Array
                    (
                        [ID] =1955
                        [Debit] =CREDIT
                        [timestamp] =1474025159
                        [LastName] =test11
                        [FirstName] =test11

                    )

                [1] =Array
                    (
                        [ID] =1956
                        [Debit] =CREDIT
                        [timestamp] =1474025150
                        [LastName] =test22
                        [FirstName] =test22
                    )
               [2] =Array
                    (
                        [ID] =1957
                        [Debit] =CREDIT
                        [timestamp] =1474025150
                        [LastName] =test33
                        [FirstName] =test33
                    )
            )
        [start_date] =2016-09-16
        [end_date] =2016-09-16
        [show_fee] =0
    )

)

Now i wanted to filter an array from the above array with specific keys. For example i want timestamp and id . We can assume id/timestampd will be key and vice versa.

I tried array_column but not succeeded.

Kapil Verma
  • 178
  • 3
  • 17

1 Answers1

0

It's not easy to be sure of what you exactly need the output to be.
A general principle might use the assumption that the following rules apply:

  • the output hierarchical structure must be the same as the one of the input
  • the id and timestamp keyed values are the only ones kept in sub-arrays where they appear
  • any other sub-arrays (i.e. where the searched keys are not present) don't change

Based on these rules, here is a general-purpose solution which works for any structure (and notably regardless of the depth-level where searched keys appear):

function select($input) {
  $keys = ['ID', 'timestamp'];
  return is_array($input)
    ? (array_keys($intersect = array_intersect_key($input, array_flip($keys))) == $keys
      ? $intersect
      : array_map('select', $input)
    )
    : $input;
}
$output = select($input);

The (tested) function uses the following approach:

  • for a given $input, returns it as is if it's not an array
  • otherwise looks for the searched keys in $input and:
    • if they're both present (this is an arbitrary choice here, might be replaced by "if at least one of them is present"), returns only them
    • otherwise returns $input mapped by recursive call of the function for each of its items
cFreed
  • 4,404
  • 1
  • 23
  • 33