0

I have a separate set of functions that grabs a "Date" and a "Time" from my application and puts the date into as a key, and the time as a multi-dimensional value.

For example purposes:

$alldatetimes = array(
    'date1' => array('13:00','14:30','14:30','14:30','15:00'),
    'date2' => array('09:00','10:00','10:30','10:30','12:00')
    );

foreach ($alldatetimes as $date => $times) {
echo '<h1>This Exports:</h1>';  
echo '<h2>'.$date.'</h2><br>';
    foreach ($times as $time) {

        echo $time.'<br>';
    }
}

This exports:
date1
13:00
14:30
14:30
14:30
15:00

date2
09:00
10:00
10:30
10:30
12:00

I'm trying to control if the time is put into the array so only one value each is in the array (I don't want 3 instances of 14:30 for that date).

Based on other posts here I tried to build something like this to identify if the value was there, but I can't figure out how to tie it all together:

function searchForId($id, $array) {
    foreach ($array as $date => $times) {
        foreach ($times as $time) { 
            if ($time === $id) {
                return $time;
            }
        }
    }
    return null;
}

Any ideas?

Update: Here is how the array is initially being created - this probably can be more efficient:

while ($schedule_q -> have_posts() ) : $schedule_q->the_post();
    $alldatetimes [get_the_date()][] = get_the_time();  
endwhile;
1
  • 2
    Are you trying to do this when you create the array so you dont add the same time twice or when you process the array with duplicate times in it Commented Apr 23, 2015 at 21:40

3 Answers 3

1

You can add a array_unique() call over each sub-array before you loop over your results to ensure it's all unique:

foreach ($alldatetimes as &$row) {
    $row = array_unique($row);
}

Output:

<h1>This Exports:</h1>
<h2>date1</h2><br>
13:00<br>
14:30<br>
15:00<br>
<h1>This Exports:</h1>
<h2>date2</h2><br>
09:00<br>
10:00<br>
10:30<br>
12:00<br>
Sign up to request clarification or add additional context in comments.

2 Comments

This was exactly what I was looking for as it cleaned the data after it was in the array. Thanks!
@CRANDT glad it helped, but generally speaking it's better practice to minimize your dataset as early as you can rather than filtering it down later on (in terms of memory usage)
0

It is not shown in your question, but how about modifying your function that builds the date/time array to use the times as keys instead of values? Using something like

$alldatetimes[$date][$time]++

in that function would give you an array with one value for each time that would be the number of occurrences of that date/time combination, like this:

$alldatetimes = array(
    'date1' => array('13:00' => 1,'14:30' => 3,'15:00' => 1),
    'date2' => array('09:00' => 1,'10:00' => 1,'10:30' => 2,'12:00' => 1)
    );

Then you could change your code that prints them out to use the key.

foreach ($times as $time => $count) {
    echo $time.'<br>';
}

1 Comment

I ended up going with this solution. I'm pretty new to PHP (low intermediate at this point) and didn't know that you could ++ when you create you array like that, especially in a multi-dimensional array.
0

You can write a recursive function

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

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.