PHP result from two arrays pairing dates where key is “Y”

问题内容:

I have two array

$array1 = array("2017-12-08","2017-12-09","2017-12-10","2017-12-11","2017-12-12","2017-12-13","2017-12-14","2017-12-15","2017-12-16","2017-12-17","2017-12-18","2017-12-19","2017-12-20","2017-12-21","2017-12-22");
$array2 = array("Y","Y","Y","Y","Y","N","N","N","Y","Y","Y","N","N","Y","Y");

I need the first and last date where the key=”Y”. Like this:

$result = array("2017-12-08 - 2017-12-12", "2017-12-16 - 2017-12-18", "2017-12-21 - 2017-12-22");

How i can make this?
I have tried with array_combine but after that I do not know how to list the dates pairing.
Thanks for helping

问题评论:

    
I not understand you question. I try to explain again a little better.
– Web28
2 hours ago

答案:

答案1:

First find only array keys that contains value as Y:

$yes = array_filter($array2, function ($yn) {return $yn == 'Y'});

Now do $result = array_intersect_key($array1, $yes) to get dates that has Y in array2.

Finally get first (current($result)) and last end($result) dates.

Example

答案评论:

答案2:

Yes this is a good idea, Justinas, but the result is not exactly what i need. Your result give me the first 2017-12-08 and the last 2017-12-22. But i need from all pairs the first and end date, like:

"2017-12-08 - 2017-12-12"
"2017-12-16 - 2017-12-18"
"2017-12-21 - 2017-12-22"

Because i need to insert in the db like

INSERT INTO $table start='firstDate' AND end='lastDate'.

And in this example 3 lines.

答案评论:

原文地址:

https://stackoverflow.com/questions/47754106/php-result-from-two-arrays-pairing-dates-where-key-is-y

添加评论

友情链接:蝴蝶教程