1

I have two arrays and I want to link together when processing them.

$dat = array(
    "2020-02-01", 
    "2020-02-05",
    "2020-02-10",
    "2020-02-12",
    "2020-02-15"
);
$word = array(
    "Attend To,Explore,Unaided,dull,bad"
); 

//User input
$start = "2020-01-01";
$end = "2020-02-07";

I want the input to affect the second array too, so when the first array gets its result from first 2, then the second array too should have it from the first 2.

//Filter out dates between start and end date
$result = array_filter($dat, function($data_item) use($start, $end) {
    return $data_item >= $start && $data_item <= $end;
});

and the result is

Array
(
    [0] => 2020-02-01
    [1] => 2020-02-05
)

I want it to be able to link $dat and $word so that the result for word too will be

Array
(
    [0] => Attend To
    [1] => Explore
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

3 Answers3

2

I don't find functional programming to be very readable/attractive for this case. Just use a simple foreach loop and conditionally grab the words related by the shared indices.

Since the two arrays share common indices, it is unnecessary work to combine the two arrays -- just reference the indices.

Code: (Demo)

$dat = ["2020-02-01", "2020-02-05", "2020-02-10", "20-02-12", "2020-02-15"];
$word = ["Attend To,Explore,Unaided,dull,bad"];
$words = explode(',', $word[0]);

//User input
$start = "2020-01-01";
$end = "2020-02-07";

$result = [];
foreach ($dat as $index => $date) {
    if ($date >= $start && $date <= $end) {
        $result[] = $words[$index];
    }
}
var_export($result);

Output:

array (
  0 => 'Attend To',
  1 => 'Explore',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    One should note that in addition to the fact it's more readable, it's also probably the most performant answer so far. It gets rid of a function overhead and passing arguments, and avoids extra operations on arrays. – Kaddath Jun 02 '20 at 08:02
1

The original keys will be maintained after array_filter, so get the entries for keys that are the same by computing the intersection. It appears that $word is a one element array with a string, so just explode it:

$word_result = array_intersect_key(explode(',', $word[0]), $result);

See a Demo.

If one of the arrays has unique values, you can combine the array and just operate on that.

$comb = array_combine(explode(',', $word[0]), $dat);

$result = array_filter($comb, function($data_item) use($start,$end) {
    return $data_item >= $start && $data_item <= $end;
});

This yields:

Array
(
    [Attend To] => 2020-02-01
    [Explore] => 2020-02-05
)

You can use the array as is or use array_keys to get the keys as the $word array.

If it's not guaranteed to be $word[0] then you can use reset($word) or current($word).

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

A possible solution assuming that the arrays have the same keys (I changed it to reflect this), is to use the constant ARRAY_FILTER_USE_BOTH to array_filter, so that the key is available in the callback function.

here i fill a second array $result2 with the words while filtering the data (note that things are added in use and $result2 is passed by reference):

$dat = array("2020-02-01","2020-02-05","2020-02-10","20-02-12","2020-02-15");
$word = array("Attend To","Explore","Unaided","dull","bad"); 

//User input
$start = "2020-01-01";
$end = "2020-02-07";

//Filter out dates between start and end date
$result2 = [];
$result = array_filter($dat, function($data_item, $key) use($start, $end, $word, &$result2) {
    if($data_item >= $start && $data_item <= $end){
        $result2[$key] = $word[$key];
        return true;
    }
    return false;
}, ARRAY_FILTER_USE_BOTH);

AbraCadaver's answer is perfect fit when only the filtering is needed, but this can be useful in case someone has to do extra operations in the filter callback..

Kaddath
  • 5,933
  • 1
  • 9
  • 23