Search for dates in an array

  • I have a region called Wedding Dates where my client who is a photographer can enter dates that are already booked. Multiple entries are enabled.

    The id of the items is booked_date

    I want to store these in an array on a php page and then search this array to see if a particular date is contained in it.

    I can output the dates to the page and they output in the format yyyy-mm-dd

    When I search through the array, I can't seem to get a match. This is the code I am using. Can anyone tell me where I am going wrong please?

    1. $bookeddates = perch_content_custom('Wedding Dates', array('skip-template'=>true,))
    2. if (in_array('2019-12-04', $bookeddates, true))
    3. {
    4. echo "Match found";
    5. }
    6. else
    7. {
    8. echo "Match not found";
    9. }
  • Hi,

    A good way to debug this is to output the array so you can examine it:

    1. echo '<pre>' . print_r($bookeddates, 1) . '</pre>';

    Or if you have debug mode turned on, you can output the array in the debug message at the bottom of the page:

    1. PerchUtil::debug($bookeddates);

    Since this is a multiple-item region, your array probably looks something like this:

    You can filter the array in PHP, but you can also filter the region items directly:

    1. $result = perch_content_custom('Wedding Dates', array(
    2. 'skip-template' => true,
    3. 'filter' => 'your_date_field_id',
    4. 'match' => 'eq',
    5. 'value' => $date,
    6. ));