I have the following array:
array('16 HOURS','13.3 HOURS','10.6 HOURS AGO','8 HOURS AGO','5.3 HOURS AGO','2.6 HOURS AGO','CURRENT')
I want to insert empty cells ('') between every string until the desired length is reached. I have tried various loops, for i etc but always end up with white spaces behind CURRENT or before 16 HOURS, thanks in advance.
Try this.
$array = array('16 HOURS','13.3 HOURS','10.6 HOURS AGO','8 HOURS AGO','5.3 HOURS AGO','2.6 HOURS AGO','CURRENT');
$i = 1;
foreach($array as $val) {
$tempArray[] = $val;
if($i < count($array)) {
$tempArray[] = '';
}
$i++;
}
print_r($tempArray);
Result
Array
(
[0] => 16 HOURS
[1] =>
[2] => 13.3 HOURS
[3] =>
[4] => 10.6 HOURS AGO
[5] =>
[6] => 8 HOURS AGO
[7] =>
[8] => 5.3 HOURS AGO
[9] =>
[10] => 2.6 HOURS AGO
[11] =>
[12] => CURRENT
)
$myArray = array(
'16 HOURS',
'13.3 HOURS',
'10.6 HOURS AGO',
'8 HOURS AGO',
'5.3 HOURS AGO',
'2.6 HOURS AGO',
'CURRENT'
);
$newArray = array_combine(
range(0,12,2),
$myArray
) +
array_fill_keys(
range(1,12,2),
''
)
;
ksort($newArray);
var_dump($newArray);
Related
Simple question here. Why is this giving an error?
for ($k = 1; $k < 17; $k++){
echo $result[0]->answer_."$k";
}
Edit: contents of $result:
Array ( [0] => stdClass Object ( [entry_id] => 37 [answer_1] => 20-24 yrs old [answer_2] => No [answer_3] => Yes [answer_4] => No [answer_5] => 236.7 [answer_6] => Ideal blood pressure – upper figure between 91 & 120, lower figure between 61& 80 [answer_7] => No, I haven’t had it checked [answer_8] => No, I quit more than 10 years ago [answer_9] => I sometimes get 150 minutes or more per week [answer_10] => 2-5 hours per day [answer_11] => Every day [answer_12] => Less often or never [answer_13] => Every day [answer_14] => 3-4 days a week [answer_15] => Never [answer_16] => imperial ) )
Try
for ($k = 1; $k < 17; $k++){
echo $result[0]->{'answer_'.$k};
}
Updated based on comment from mpen below
I don't want to return DATE (Y-m-d).
I need to print all days until end of month from a given day independently of the month or year.
I tried both [$array as $i] - [$array as $key] and didn't work.
$myday (for example = 19)
return $days
would result:
Array
[0] => 20
[1] => 21
[2] => 22
[3] => 23
...
[31] => 31 || [30] => 30 || [28] => 28
I would need each value for $days to compare each to another field.
Didn't try to use $myday as regular number instead of treating as date. And not use strtotime, mktime....
EDITING
Need something very simple like this one:
$output = array();
for ($i=$myday+1;$i<=31 || $i<=30 || $i<=28;$i++) {
$output[] = $i;
}
But print_r won't do it, I need to return as each value to use in different if conditions
This is easily done using DateTime(), DateInterval(), DatePeriod(), and relative date formats.
$start = (new DateTime())->setDate(date('Y'), date('m'), $myday + 1);
$end = new DateTime('first day of next month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
$days = array();
foreach($period as $date) {
$days[] = $date->format('d');
}
Results
Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
[8] => 28
[9] => 29
[10] => 30
[11] => 31
)
Demo
I have a script that builds an array of week numbers for the last 12 weeks like so:
$week_numbers = range(date('W'), date('W')-11, -1);
However, if the current week number is 1, then this will return an array like so:
Array
(
[0] => 1
[1] => 0
[2] => -1
[3] => -2
[4] => -3
[5] => -4
[6] => -5
[7] => -6
[8] => -7
[9] => -8
[10] => -9
[11] => -10
)
But I need this array to look like this instead:
Array
(
[0] => 1
[1] => 52
[2] => 51
[3] => 50
[4] => 49
[5] => 48
[6] => 47
[7] => 46
[8] => 45
[9] => 44
[10] => 43
[11] => 42
)
Can anyone see a simple solution to this?
I have thought about doing something like this (not tested):
$current_week_number = date('W');
if($current_week_number<12){
// Calculate the first range of week numbers (for current year)
$this_year_week_numbers = range(date('W'), 1, -1);
// Calculate the next range of week numbers (for last year)
$last_year_week_numbers = range(52, 52-(11-$current_week_number), -1);
// Combine the two arrays to return the week numbers for the last 12 weeks
$week_numbers = array_merge($this_year_week_numbers,$last_year_week_numbers);
}else{
// Calculate the week numbers the easy way
$week_numbers = range(date('W'), date('W')-11, -1);
}
one idea
$i = 1;
while ($i <= 11) {
echo date('W', strtotime("-$i week")); //1 week ago
$i++;
}
if you arent scared of loops you can do this:
$week_numbers = range(date('W'), date('W')-11, -1);
foreach($week_numbers as $key => $value) { if($value < 1) $week_numbers[$key] += 52; }
You can do a modulo % trick:
$week_numbers = range(date('W'), date('W')-11, -1);
foreach ($week_numbers as $i => $number) {
$week_numbers[$i] = (($week_numbers[$i] + 52 - 1) % 52) + 1;
}
// -1 +1 is to change the range from 0-51 to 1-52
I've found that using modulo like this is often useful for date calculations, you can something similar for months, using 12.
Well, I think the easiest way is to create array after getting dates:
$week_numbers = array_map(function($iDay)
{
return ($iDay+52)%52?($iDay+52)%52:52;
}, range(date('W'), date('W')-11));
-note, that you can not do just % since 52%52 will be 0 (and you want 52)
I have an array which represent all month. How can I find today's day in the array, take that day, plus next six days (not necessary six days) and remove rest from the array?
For example:
$days = array('1', '2', '3', ... '28', '29', '30');
I need 3-9. How can I do this?
I think you should look at another way of doing whatever it is you want to do. But here is a solution for what you asked:
$days = range(1, date('t'));
$days_forward = 6;
$key = array_search(date('j'), $days);
if($key === FALSE)
die('Date not found in array');
$days = array_slice($days, $key, $days_forward + 1);
print_r($days);
Output:
Array
(
[0] => 19
[1] => 20
[2] => 21
[3] => 22
[4] => 23
[5] => 24
[6] => 25
)
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]