I have this array but I do not know how to populate it dynamically
My code:
for ($j = 1; $j <= $weeks; $j++)
{
for ($i = 1; $i <= 7; $i++)
{
$turn_data = $request->input('turn_'.$i.'_'.$j);
if(isset($turn_data))
{
$turn_data = explode("_", $turn_data);
$turn = Turn::find($turn_data[0]);
if($day < 10)
{
$day = "0".$day;
}
$data = array("apiKey" => "85526dd10b9aa01ae6e56698b848d191",
"turnos" => [["codigo" => $rut,
"nombreTurno" => $turn->name,
"fechaInicio" => $year."-".$month_detail_number."-".$day." 00:00:00",
"fechaTermino" => $year."-".$month_detail_number."-".$day." 23:59:59"]]);
}
$day = $day + 1;
}
}
How can I put the array? The problem is that turnos is an array too, that's my problem
I need an output like this:
Array ( [apiKey] => 85526dd10b9aa01ae6e56698b848d191 [turnos] => Array ( [0] => Array ( [codigo] => 15918421 [nombreTurno] => 6x1 FT/ 08:30 a 16:00 [fechaInicio] => 2019-12-13 00:00:00 [fechaTermino] => 2019-12-13 23:59:59 ) ) )
Thanks!
I need a way to check if there are events that overlap each other. So I made an array with the start and end hour of every event. It looks like this:
Array
(
[0] => Array
(
[start] => 0930
[end] => 1200
)
[1] => Array
(
[start] => 1000
[end] => 1230
)
[2] => Array
(
[start] => 1300
[end] => 1530
)
)
This is what I've tried to check if there are events that overlap:
if ( $orders->have_posts() ):
while ($orders->have_posts()) : $orders->the_post();
foreach($order as $o){
$start = $o['start'];
$end = $o['end'];
$attractieID = $o['attractie']->ID;
foreach($order as $key => $attractieID){
$slots[] = array('start' => $start, 'end' => $end);
if($start < $end){
//overlap
}else{
//no overlap
}
}
}
endwhile;
endif;
But this will always give true since I am checking the start and end date of the same item in my array.
I need to way to compare the start value of the current array item and the end value of the previous array item
Anyone knows if this is even possible?
Many thanks in advance!
Start looping at index 1, and compare the start time of the current event with the end of the event with index-1.
$count = count($order);
for ($i = 1; $i < $count; $i++) {
if ($order[$i]['start'] < $order[$i-1]['end']) {
// overlap
} else {
// no overlap
}
}
If you want to do this while also copying from $order to slots, you can use a variable to hold the end time from the previous iteration.
$prevEnd = null;
foreach($order as $o){
$start = $o['start'];
$end = $o['end'];
$attractieID = $o['attractie']->ID;
$slots[] = array('start' => $start, 'end' => $end);
if($prevEnd !== null && $start < $prevEnd){
//overlap
}else{
//no overlap
}
$prevEnd = $end;
}
DEMO
Try this code, with a for loop instead of foreach:
for($i=0;$i<count($order);i++){
$slots[] = array('start' => $start, 'end' => $end);
if($order[$i+1]){
if($order[$i]['start'] < $order[$i+1]['end']{
//overklap
}else{
//no overlap
}
}
Just use a normal for loop. And, to be sure everything is right, sort the array before checking it (if you are sure it is gona be sorted, you can skip that part)
$sortedOrders = usort($order, function($a, $b)
{
$aStart = $a['start'];
$bStart = $b['start'];
return ($a < $b) ? -1 : 1;
});
$count = count($sortedOrders);
for($i = 1; $i < $count; $i++)
{
if($sortedOrders[$i - 1]['end'] > $sortedOrders[$i]['start'])
{
// Handle overlap
}
}
Hi i am trying to create a sub array from an array.i.e; think I have an array such as given below
$array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}
which I explode and assign it to a variable $i..
and run the for loop as shown below..
for ( $i=0;$i<count($array);$i++) {
$a = array();
$b = $array[$i];
for($j=0;$j<count($array);$j++){
if($b != $array[$j]){
$a[] = $array[$j];
}
}
the output I want is when
$i = 1
the array should be
{2,3,4,5,6,7,8,9,10,11}
and when
$i = 2
the array should be
{3,4,5,6,7,8,9,10,11,12}
similarly when
$i=19
the array should be
{1,2,3,4,5,6,7,8,9,10}
so how can I do it.
Assuming $i is supposed to be an offset and not the actual value in the array, you can do
$fullArray = range(1, 19);
$i = 19;
$valuesToReturn = 10;
$subset = iterator_to_array(
new LimitIterator(
new InfiniteIterator(
new ArrayIterator($fullArray)
),
$i,
$valuesToReturn
)
);
print_r($subset);
This will give your desired output, e.g.
$i = 1 will give 2 to 11
$i = 2 will give 3 to 12
…
$i = 10 will give 11 to 1
$i = 11 will give 12 to 2
…
$i = 19 will give 1 to 10
$i = 20 will give the same as $i = 1 again
and so on.
$array = range(1, 19);
$i = 19;
$result = array();
$after = array_slice($array, $i, 10);
$before = array_slice($array, 0, 10 - count($after));
$result = array_merge($after, $before);
var_dump(json_encode($result));
P.S. please note 0 element has 1 value and so on...
for ($i = 0; $i < count($array); $i++) {
if ($i + 10 < count($array))
$a = array_slice($array, $i, 10);
else
$a = array_merge(array_slice($array, $i), array_slice($array, 0, 10-(count($array)-$i)));
// do something with $a before it is over-written on the next iteration
}
This test:
<?php
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
for ($i = 0; $i < count($array); $i++) {
if ($i + 10 < count($array))
$a = array_slice($array, $i, 10);
else
$a = array_merge(array_slice($array, $i), array_slice($array, 0, 10-(count($array)-$i)));
echo "<h2>$i</h2>\n<pre>".print_r($a,true)."</pre><br />\n";
}
Resulted in this:
0
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
...
9
Array
(
[0] => 10
[1] => 11
[2] => 12
[3] => 13
[4] => 14
[5] => 15
[6] => 16
[7] => 17
[8] => 18
[9] => 19
)
10
Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
[4] => 15
[5] => 16
[6] => 17
[7] => 18
[8] => 19
[9] => 1
)
...
18
Array
(
[0] => 19
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
)
This works fine from my end
<?php
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
$size = sizeof($array); // Defining the array size
$str = 17; // This is the reference value from which you have to extract the values
$key = array_search($str, $array);
$key = $key+1; // in order to skip the given reference value
$start = $key%$size;
$end = $start+9;
for($i=$start; $i<=$end; $i++) {
$j = ($i%$size);
$result[] = $array[$j];
}
echo '<pre>'; print_r($result);
?>
It looks like all you need is a slice of a certain size from the array, slice that wraps around the array's end and continues from the beginning. It treats the array like a circular list.
You can achieve this in many ways, one of the simplest (in terms of lines of code) is to extend the original array by appending a copy of it at its end and use the PHP function array_slice() to extract the slice you need:
function getWrappedSlice(array $array, $start, $count = 10)
{
return array_slice(array_merge($array, $array), $start, $count);
}
Of course, you have to be sure that $start is between 0 and count($array) - 1 (including), otherwise the value returned by the function won't be what you expect.
Round-robin on an array can be achieved by doing a "rotate" operation inside each iteration:
for ($i = 0; $i < count($array); ++$i) {
// rotate the array (left)
array_push($array, array_shift($array));
// use $array
}
During the loop, the first element of the array is placed at the back. At the end of the loop, the array is restored to its original value.
I'm making a function that would break current month into an array of weeks, each week with its starting and ending date. For example the current month is September, so this is what I expect my function to return:
$week_arr[1]['start'] = '1-09-2014';
$week_arr[1]['end'] = '7-09-2014';
$week_arr[2]['start'] = '8-09-2014';
$week_arr[2]['end'] = '14-09-2014';
/*
.
.
.
*/
$week_arr[5]['start'] = '29-09-2014';
$week_arr[5]['end'] = '30-09-2014';
I understand that only non-leap February will have 4 weeks.
This is what I have so far
$no_of_days_in_month = date("t");
$no_of_weeks = (ceil(intval($no_of_days_in_month)/7));
$week_inc = 0;
$curr_month_year = date("m-Y");
$week_arr = array();
for($i = 1; $i <= $no_of_weeks; $i++){
$week_arr[$i]['start'] = ($week_inc+1)."-".$curr_month_year;
$week_arr[$i]['end'] = ($week_inc+7)."-".$curr_month_year;
$week_inc += 7;
}
var_dump($week_arr);
It works almost correctly, except I want it to stop on the last day of the month in the last week.
Above code has a problem with the 5th index of the week. It results
$week_arr[5]['start'] = '29-09-2014'
$week_arr[5]['end'] = '35-09-2014'
How to fix that?
Thank you for your answers.
You could add a check to the start of your for loop to stop processing if the end date is greater than the number of days in the month:
if($week_inc + 7 > $no_of_days_in_month)
break;
Output
Or if you still want the partial week at the end, you could use a similar check to define what the last day should be:
for($i = 1; $i <= $no_of_weeks; $i++){
if($week_inc + 7 > $no_of_days_in_month)
$day = $no_of_days_in_month;
else
$day = $week_inc + 7;
$week_arr[$i]['start'] = ($week_inc+1)."-".$curr_month_year;
$week_arr[$i]['end'] = $day."-".$curr_month_year;
$week_inc += 7;
}
You'll then get a partial week at the end:
[5] => Array
(
[start] => 29-09-2014
[end] => 30-09-2014
)
Another option would be to use a DatePeriod class for this one too. Example:
$begin = new DateTime('first day of ' . date('Y-m-d'));
$end = new DateTime('last day of ' . date('Y-m-d'));
$interval = new DateInterval('P1W');
$daterange = new DatePeriod($begin, $interval, $end);
$dates = array();
foreach($daterange as $key => $date) {
$check = ($date->format('W') != $end->modify('last day of this month')->format('W')) ? '+6 days' : 'last day of this week';
$dates[$key+1] = array(
'start' => $date->format('Y-m-d'),
'end' => $date->modify($check)->format('Y-m-d'),
);
}
echo '<pre>';
print_r($dates);
Should yield this:
Array
(
[1] => Array
(
[start] => 2014-09-01
[end] => 2014-09-07
)
[2] => Array
(
[start] => 2014-09-08
[end] => 2014-09-14
)
[3] => Array
(
[start] => 2014-09-15
[end] => 2014-09-21
)
[4] => Array
(
[start] => 2014-09-22
[end] => 2014-09-28
)
[5] => Array
(
[start] => 2014-09-29
[end] => 2014-09-30
)
)
I have looked and googled many times I found a few posts that are simular but I can not find the answer Im looking for so I hope you good people can help me.
I have a function that returns a simple number array. The array number values are dynamic and will change most frequently.
e.g.
array(12,19,23)
What I would like to do is take each number value in the array, compare it to a set range and return all the lower value numbers up to and including the value number in the array.
So if I do this:
$array = range(
(11,15),
(16,21),
(22,26)
);
The Desired output would be:
array(11,12,16,17,18,19,22,23)
But instead I get back all the numbers in all the ranges.
array(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26)
What would be a simple solution to resolve this?
Try this code
$range = array(
array(11,15),
array(16,21),
array(22,26),
);
$array = array(12,19,23);
$result = array();
foreach($range as $key=>$value)
{
//$range1 =$range[$key];
$min = $range[$key][0];
$max = $range[$key][1];
for($i = $min;$i<=$max;$i++)
{
if($i <= $array[$key])
{
array_push($result,$i);
}
}
}
echo "<pre>";print_r($result);
Iterate over each element, find the the start and end values you need to include, and append them to the output array:
$a = array(12,19,23);
$b = array(
range(11,15),
range(16,21),
range(22,26)
);
$c = array();
foreach ($a as $k => $cap) {
$start = $b[$k][0];
$finish = min($b[$k][count($b[$k])-1], $cap);
for ($i = $start; $i <= $finish; $i++) {
$c[] = $i;
}
}
print_r($c);
prints
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)
My solution is probably not the most efficient, but here goes:
$numbers = array(12,19,23);
$ranges = array(
array(11,15),
array(16,21),
array(22,26)
);
$output = array();
// Loop through each of the numbers and ranges:
foreach($numbers as $num) {
foreach($ranges as $r) {
if ($num >= $r[0] && $num <= $r[1]) {
// This is the correct range
// Array merge to append elements
$output = array_merge($output, range($r[0], $num));
break;
}
}
}
// Sort the numbers if you wish
sort($output, \SORT_NUMERIC);
print_r($output);
Produces:
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)