1

I have the following PHP code working to identify intersections of dates that fall on dates where we have a Full condition.

Based on this StackOverflow post :
php check multiple dates in array are within a date range
I have a working filter using a test array ($d2).

<?php

$d2 = array ('08/23/2019','08/24/2019','08/25/2019','08/26/2019','08/27/2019','08/28/2019','08/29/2019','08/30/2019','08/31/2019','09/01/2019');

$start = strtotime('08/27/2019');
$end =   strtotime('08/29/2019');

foreach($d2 AS $date) {
    $timestamp = strtotime($date);
    if($timestamp >= $start && $timestamp <= $end) {
        echo "The date $date falls on a day where we are sold out \n";
    } else {
        // echo "Process Reservation \n";
    }
}
?>

The print_r($d2) that works looks like this:

Array
(
    [0] => 08/23/2019
    [1] => 08/24/2019
    [2] => 08/25/2019
    [3] => 08/26/2019
    [4] => 08/27/2019
    [5] => 08/28/2019
    [6] => 08/29/2019
    [7] => 08/30/2019
    [8] => 08/31/2019
    [9] => 09/01/2019
)

But my array which is comprised of checking existing dates, then finding which have x number of instances on a particular date. For instance, if we have 7 or more we have a full condition (sold out).

$sold_outUS = array_filter($datesUS, function($n) { return $n >= 7; });

If I print_r($sold_outUS) this array, it shows a different type of array structure:

Array
(
    [08/23/2019] => 7
    [08/24/2019] => 7
    [08/25/2019] => 7
    [08/26/2019] => 7
    [08/27/2019] => 7
    [08/28/2019] => 7
    [08/29/2019] => 7
    [08/30/2019] => 7
    [08/31/2019] => 7
    [09/01/2019] => 7
)

How do I change the way this array is stored so it matches the working state and what is happening that I need to learn?

Looks as though I need an independent key and value for each instance in the array. Just don’t know how to change or reset the array to be? non-multi-dimensional?

7
  • 2
    You just want the keys of that array? Use array_keys(). Commented Aug 30, 2019 at 0:06
  • what is the value of $datesUS? Commented Aug 30, 2019 at 0:09
  • In the examples, there are two different results shown. The working version is a simple array that shows the default numeric key and the unique value which is a date. The one the does NOT work looks to be a single key then each Date and the qty (=> 7) which is not needed. Commented Aug 30, 2019 at 0:15
  • It's not clear what you're asking. You want to know how to create an array that holds multiple values for each date? Commented Aug 30, 2019 at 0:34
  • Your array is not multidimensional, so why are you asking how to make it non-multidimensional? Did you mean "non-associative"? Commented Aug 30, 2019 at 0:37

1 Answer 1

1

Use array_keys to get the keys of an associative array.

$sold_outUS = array_keys(array_filter($datesUS, function($n) { return $n >= 7; }));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.