A
A
Alex2018-02-19 21:23:01
PHP
Alex, 2018-02-19 21:23:01

How to parse arrays according to the principle of who is the last one and slippers?

Greetings!
I probably already got everyone with these arrays.
The bottom line is that there are arrays of dates with values ​​​​(values ​​\u200b\u200bcan be anything, for example, the alphabet)
It is necessary to parse them somehow, provided that the next array is always true and replaces / divides the previous one.
I will show clearly.
Initial data example

array:3 [
  0 => array:6 [
    "2018-03-05" => "a"
    "2018-03-06" => "b"
    "2018-03-07" => "c"
    "2018-03-08" => "d"
    "2018-03-09" => "e"
    "2018-03-10" => "f"
  ]
  1 => array:4 [
    "2018-03-07" => "g"
    "2018-03-08" => "h"
    "2018-03-09" => "i"
    "2018-03-10" => "k"
  ]
  2 => array:8 [
    "2018-03-01" => "l"
    "2018-03-02" => "m"
    "2018-03-03" => "n"
    "2018-03-04" => "o"
    "2018-03-05" => "p"
    "2018-03-06" => "q"
    "2018-03-07" => "r"
    "2018-03-08" => "s"
  ]
  3 => array:3 [
    "2018-03-04" => "t"
    "2018-03-05" => "v"
  ]
]

How to achieve such a result? (the order of arrays is not important)
Drive recursions?
array:3 [
  0 => array:3 [
    "2018-03-01" => "l"
    "2018-03-02" => "m"
    "2018-03-03" => "n"
  ]
 1 => array:2 [
    "2018-03-04" => "t"
    "2018-03-05" => "v"
  ]
 2 => array:3 [
    "2018-03-06" => "q"
    "2018-03-07" => "r"
    "2018-03-08" => "s"
  ]
 3 => array:2 [
    "2018-03-09" => "i"
    "2018-03-10" => "k"
  ]
]

I've already broken my head ...
As I understand it: we take the last array, run the dates through all other arrays.
If we find an intersection, then we split ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
grinat, 2018-02-19
@grinat

Do recursions, php 7 might even have support for tail recursions.

V
Vladimir Olohtonov, 2018-02-19
@sgjurano

Get the data structure "set".
Go through the external list in reverse order, for each element of the nested lists, check whether there is such an element in the set, if not, add it to the set and to the resulting list.
It is not clear on what basis you break the elements into lists.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question