I have a multidimensional array and it is like this:
Array
(
[Monday] => Array
(
[open] => 05.00 PM
[close] => 04.00 PM
[state] => 0
)
[Tuesday] => Array
(
[open] =>
[close] =>
[state] => 1
)
[Wednesday] => Array
(
[open] => 03.00 AM
[close] => 06.00 PM
[state] => 0
)
[Thursday] => Array
(
[open] =>
[close] =>
[state] => 1
)
[Friday] => Array
(
[open] => 05.00 PM
[close] => 03.00 PM
[state] => 0
)
[Saturday] => Array
(
[open] => 05.00 PM
[close] => 06.00 PM
[state] => 0
)
[Sunday] => Array
(
[open] =>
[close] =>
[state] => 1
)
)
Using this array I want to create output like this:
Monday - 05.00 PM - 04.00 PM
Tuesday - Closed
Wednesday - 03.00 AM - 06.00 PM
Thursday - Closed
Friday - 05.00 PM - 03.00 PM
Saturday - 05.00 PM - 06.00 PM
Sunday - Closed
I tried it with 2 foreach loops, but I couldn't get it to show the expected output.
foreach ($result as $days => $values) {
echo "$days";
foreach ($values as $k) {
echo " - $k";
}
echo "<br/>";
}
Its output is similar to this:
Monday - 05.00 PM - 04.00 PM - 0
Tuesday - - - 1
Wednesday - 03.00 AM - 06.00 PM - 0
Thursday - - - 1
Friday - 05.00 PM - 03.00 PM - 0
Saturday - 05.00 PM - 06.00 PM - 0
Sunday - - - 1
Can anybody tell me how can I figure this out?
NOTE: if state = 0 it doesn't need to display and state = 1 it should be Closed
Do the foreach loop like that:
foreach ($result as $days => $values) {
echo $days." - ";
echo $values["state"] ? "Closed" : $values["open"]." - ".$values["close"];
echo "<br />";
}
You should check state index exist(1) or not(0) like: if ($values['state'])
Try this one:
foreach ($result as $days => $values) {
echo "$days";
if ($values['state']) {
echo " - Closed";
} else {
echo ": ".$values['open']." - ".$values['close'];
}
echo "<br/>";
}
Do foreach loop with html table to get currect aligned line
$data = "<table >";
foreach($result as $day=>$value) {
if($value["state"]==1) {
$value = "<td colspan='3'> Closed </td>";
} else {
$value = "<td> $value[open] </td><td> - </td><td> $value[close] </td>";;
}
$data .= "<tr><td> $day </td><td> - </td> $value</tr>";
}
$data .= "</table>";
echo $data;
Related
I have an input array which contains arrays of day and time data:
$temp = Array
(
[0] => Array
(
[day] => Tuesday
[time] => 07:44 pm - 08:44 pm
)
[1] => Array
(
[day] => Tuesday
[time] => 04:00 am - 04:25 am
)
[2] => Array
(
[day] => Sunday
[time] => 04:00 am - 04:25 am
)
[3] => Array
(
[day] => Sunday
[time] => 04:00 am - 04:25 am
)
[4] => Array
(
[day] => Friday
[time] => 04:00 am - 04:25 am
)
[5] => Array
(
[day] => Friday
[time] => 04:00 am - 04:25 am
)
)
Now I want to group the common times display as one element and if the time is the same for more than one day then it's should display one entry. So what is the best way to achieve the desired result without making this too complex?
Array
(
[0] => Array
(
[day] => Tuesday
[time] => 04:00 am - 04:25 am & 07:44 pm - 08:44 pm
)
[1] => Array
(
[day] => Friday & Sunday
[time] => 04:00 am - 04:25 am
)
)
Here it's what I have done:
$final = [];
foreach ($temp as $val) {
$final[strtolower($val['day'])][] = $val['time'];
}
foreach ($final as $k => $v) {
sort($v);
$v = array_unique($v);
$last = array_pop($v);
$final[$k] = [
'day' => $k,
'time' => count($v) ? implode(", ", $v) . " & " . $last : $last,
];
}
There are 4 basic steps:
Remove duplicate rows.
Sort all rows by day (via lookup) ASC, then time (as a string) ASC.
Store joined time values using day values as temporary keys.
Store joined day values using joined time values as temporary keys.
Code: (Demo)
$array=[
['day'=>'Tuesday','time'=>'07:44 pm - 08:44 pm'],
['day'=>'Tuesday','time'=>'04:00 am - 04:25 am'],
['day'=>'Sunday','time'=>'04:00 am - 04:25 am'],
['day'=>'Sunday','time'=>'04:00 am - 04:25 am'],
['day'=>'Friday','time'=>'04:00 am - 04:25 am'],
['day'=>'Friday','time'=>'04:00 am - 04:25 am']
];
$array=array_unique($array,SORT_REGULAR); // remove exact duplicate rows
$order=array_flip(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']); // lookup array
usort($array,function($a,$b)use($order){ // sort by day name ASC then time ASC
if($order[$a['day']]==$order[$b['day']]){
return $a['time']<=>$b['time']; // 2nd sort criteria: time string
}
return $order[$a['day']]<=>$order[$b['day']]; // 1st sort criteria: day name via lookup array
});
foreach($array as $row){
if(!isset($temp[$row['day']])){
$temp[$row['day']]=$row['time']; // store time for first occurring day
}else{
$temp[$row['day']].=" & {$row['time']}"; // join times (for pre-existing day) as they are found
}
}
foreach($temp as $day=>$times){
if(!isset($result[$times])){
$result[$times]=['day'=>$day,'time'=>$times]; // store first occurring day and time using time as temp key
}else{
$result[$times]['day'].=" & $day"; // join day names as they are found
}
}
var_export(array_values($result)); // optionally remove the temporary keys
Output:
array (
0 =>
array (
'day' => 'Tuesday',
'time' => '04:00 am - 04:25 am & 07:44 pm - 08:44 pm',
),
1 =>
array (
'day' => 'Friday & Sunday',
'time' => '04:00 am - 04:25 am',
),
)
Here is another version that doesn't call array_unique(), but I don't like it as much because it does iterated sort() calls and generates a deeper temporary array.
foreach($array as $row){
$temp[$row['day']][$row['time']]=$row['time']; // remove duplicates and group by day
}
$order=array_flip(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']); // lookup array
uksort($temp,function($a,$b)use($order){
return $order[$a]<=>$order[$b]; // sort by day name via lookup array
});
foreach($temp as $day=>$times){
sort($times); // sort time elements ASC
$time_key=implode(' & ',$times); // join the day's time elements
if(!isset($result[$time_key])){ // if first occurrence of the time string
$result[$time_key]=['day'=>$day,'time'=>$time_key]; // store data using time string as temp key
}else{
$result[$time_key]['day'].=" & {$day}"; // concatenate new day to day element
}
}
var_export(array_values($result));
Hi i want to show Opening hours which i'm getting dynamically through database. here is the array of that
Array
(
[from] => Array
(
[0] => 09:30
[1] => 09:30
[2] => 09:30
[3] => 09:30
[4] => 09:30
[5] => 09:30
[6] => 09:30
)
[to] => Array
(
[0] => 18:30
[1] => 18:30
[2] => 18:30
[3] => 18:30
[4] => 18:30
[5] => 18:30
[6] => 18:30
)
)
Now what i want is if the time is equals to fri then it will show the time Mon-Fri and then Sat Sun closed or if the time is same till Saturday then it will show Mon-Sat and if the whole week time is same then it will show Mon-Sun. Hope i am able to tell you exactly what i want. This is my PHP code
Updated Answer
$result = array_unique($time['from']);
if (count(array_unique($time['from'])) == 3)
{
echo '<li><strong>Mon-Fri:</strong><span>' . $time['from'] . ' to ' . $time['to'] . '</span></li>
<li>
<strong>Sat-Sun:</strong>
<span>'. $time['from'][6] .'</span>
</li>';
} else if(count(array_unique($time['from'])) == 2)
{
echo '<li><strong>Mon-sat:</strong><span>' . $time['from'][0] . ' to ' . $time['to'][0] . '</span></li>
<li>
<strong>Sun:</strong>
<span>'. $time['from'][6] .'</span>
</li>';
}
else if(count(array_unique($time['from'])) == 1) {
echo '<li><strong>Mon - Sun:</strong><span>' . $time['from'][0] . ' to ' . $time['to'][0] . '</span></li>';
}
I have the variable $alldates, which with print_r brings me the following:
Array ( [0] => Array ( [0] => 24. May 2016 08:30 - 17:00 [1] => 7. June 2016 08:30 - 17:00 ) )
I need to show the dates (which can be a lot more than two) in PDF, ordered under eachother like:
24. May 2016 08:30 - 17:00
7. June 2016 08:30 - 17:00
How to get the dates from the array and display them?
foreach ($alldates as $index){
echo $index."<br/>";
}
i'm not sure about your array , alternative way :
foreach ($alldates[0] as $index){
echo $index."<br/>";
}
Try with this:
foreach ($alldates as $date) {
echo $date . '<br />';
}
or here thre is another way
$count = count($alldates);
for ($i = 0; $i < $count; $i++) {
echo $alldates[i] . '<br />';
}
Nested loop can be used if its multi-dimensional array like this :
Array
(
[0] => Array
(
[0] => 24. May 2016 08:30 - 17:00
[1] => 7. June 2016 08:30 - 17:00
)
[1] => Array
(
[0] => 24. May 2016 08:30 - 17:00
[1] => 7. June 2016 08:30 - 17:00
)
)
foreach ($alldates as $s_date){
foreach ($s_date as $n_date){
echo $n_date."<br/>";
}
}
I want to build a movie timetable. I have $array1 containing the movie titles and airing dates and times:
array =
0: array =
Title: string = American Beauty
Date: string = 25/09/2012
Time: string = 15:00 - 16:20
1: array =
Title: string = The Godfather
Date: string = 25/09/2012
Time: string = 16:20 - 18:20
2: array =
Title: string = Pulp Fiction
Date: string = 26/09/2012
Time: string = 15:00 - 16:20
And I have $array2 containing the days of the month grouped by Mondays, Tuesday s, Wednesday s, Thursday s and Fridays (no movies during the weekend)
Array
(
[1] => Array
(
[0] => 3
[1] => 10
[2] => 17
[3] => 24
[4] =>
)
[2] => Array
(
[0] => 4
[1] => 11
[2] => 18
[3] => 25
[4] =>
)
[3] => Array
(
[0] => 5
[1] => 12
[2] => 19
[3] => 26
[4] =>
)
[4] => Array
(
[0] => 6
[1] => 13
[2] => 20
[3] => 27
[4] =>
)
[5] => Array
(
[0] => 7
[1] => 14
[2] => 21
[3] => 28
[4] =>
)
)
I need to intersect these two arrays so I can print under day 25 the movie “American Beauty” also under day 25 “The Godfather” and under day 26 “Pulp Fiction”.
Meaning I need to print:
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3 4 5
10 11 12
17 18 19
24 25 26
15:00-16:20 American Beauty 15:00-16:20 Pulp Fiction
16:20-18:20 The Godfather
My tries so far:
foreach( $array1 as $key => $value )
{
$theTime = $value['Time'];
$theTime = explode("/", $theTime );
$days[] = $theTime [0];
$months[] = $theTime [1];
}
So I have all the airing days in array $days but from here I don’t know how to follow or even if this approach is the correct one.
Here's how I get $array2:
$month = 9;
$year = 2012;
for ($i = 1; $i <= 31; $i++)
{
$timestamp = mktime(0, 0, 0, $month , $i, $year);
if (date("n", $timestamp) == $month )
{
$day = date("N", $timestamp);
// Monday 1 to Friday 5
if ($day == 1 OR $day <= 5) {
$array2[$day][] = date("j", $timestamp);
}
}
}
Please help, I’m stuck.
Thanks very much
Ok you will iterate over $array1 and parse the date of the movie.
Then, you will look inside array2 if your day is there
foreach($array1 as $movie){
$movieDate = new DateTime($movie);
//as you indexed $array2 with monday = 1 -> friday = 5 you can use the "w" format
if(isset($array2[$movieDate->format("w"))){
$array[$movieDate->format("j")][] = $movie;
}
}
I made some mutation in your output array, it becomes :
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3=>[] 4 =>[] 5=>[]
10=>[] 11 =>[] 12=>[]
17=>[] 18 =>[] 19=>[]
24 =>[] 25=>[ 26 =>[
15:00-16:20 American Beauty, 15:00-16:20 Pulp Fiction]
16:20-18:20 The Godfather]
I have an array like this:
Array
(
[0] => stdClass Object
(
[Start] => 08:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Math
)
[1] => stdClass Object
(
[Start] => 10:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Geography
)
[2] => stdClass Object
(
[Start] => 14:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Science
)
[3] => stdClass Object
(
[Start] => 10:00
[dayName] => Thursday
[dayID] => 4
[courseName] => Math
)
[4] => stdClass Object
(
[Start] => 18:00
[dayName] => Friday
[dayID] => 5
[courseName] => History
)
)
What I want to do is , I want to compare the daya nd time now to the values in the array. For example lets assume that it is 7:00 am and it is Tuesday. Then I want to get the Object[0]. But if it is 11:00 o'clock then i need to get the Object[2] which starts at 14:00 on Tuesday.
It it is Tuesday and 16:00 o'clock then i need Object[3] .
If it is a weekend then i need the beginning of the week which is Tuesday at 08:00 with the Math Course.
I tried to get that using key => value but I mixed up.
How can I compare the Day and then time and in case there is a Course on that combination just pick it up if not just continue checking.
regards
littleblue
function getObject($array){
$timeNow = date('U'); // time now
$oneHour = $timeNow+3600; // time now + 1 hour
foreach($array as $num => $one){ // loop in each $array
$date = strtotime($one->Start); // convert start time to timestamp
if($date >= $timeNow && $date < $oneHour && date('l', $date) == $one->dayName){ // if all criteria met
return $array[$num]; // return that object
}
}
return array('no data'); // if no criteria is met return no data.
}
$course = getObject($yourArray);
echo $course->courseName;
You can mix the use of DateTime and a simple min search :
$myCourse = null;//initialisation
$myCourseDate =null;
$now = new DateTime;
foreach($array as $course){
//get the date from the datas
$date = DateTime::createFromFormat('l h:i',$course->dayName.' '.course->start);
if($now < $date && ($myCourseDate === null || $myCourseDate > $date)){
$myCourse = $course;
$myCourseDate = $date;
}
}
//at the end of the loop, you've got the expected course