PHP reorder indexes nested array - php

I've this array, that i want to reoder indexes of "Mer" elements.
Array
(
[Tennis 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[3] => 09:00
[4] => 10:00
[5] => 11:00
[6] => 12:00
[7] => 13:00
[8] => 14:00
[9] => 15:00
[10] => 16:00
[11] => 17:00
[12] => 18:00
)
)
)
[Padel 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[0] => 05:00
[1] => 06:00
[2] => 07:00
[3] => 08:00
[4] => 09:00
[5] => 10:00
[6] => 11:00
)
)
)
)
Array
(
[Tennis 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[3] => 09:00
[4] => 10:00
[5] => 11:00
[6] => 12:00
[7] => 13:00
[8] => 14:00
[9] => 15:00
[10] => 16:00
[11] => 17:00
[12] => 18:00
)
)
)
[Padel 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[4] => 09:00
[5] => 10:00
[6] => 11:00
)
)
)
)
This array is back from
unset($DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]][$index]);
so i need to reorder this: $DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]], but something was wrong in my concept.
I've tried array_values() for this multidimensional array but that was not good.
Any idea is apprectiated.

You are correct in wanting to use array_values to reorder indexes. Based on your array structure, this should work:
$DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]] = array_values($DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]]);

Related

Foreach loop on smarty not returning what I expect

I am trying to use foreach loop on smarty to list all the lockers/carriers from an array. I suspect there might be something wrong with the array but I can't pinpoint the problem.
My code is:
{foreach from=$delivery_points.lockers item=carrier name=myLoop}
<option value="{$carrier->id}"
{if $carrier->id eq $selected_deliveryID } selected {/if}
> {$carrier.address->name|escape:'htmlall':'UTF-8'}
{$carrier->street|escape:'htmlall':'UTF-8'}
{$carrier->postalCode|escape:'htmlall':'UTF-8'}
<!-- {* {$carrier->City|escape:'htmlall':'UTF-8'} *} -->
</option>
{/foreach}
My array is:
Array ( [lockers] => Array ( [0] => Array ( [id] => BOX2756 [address] => Array ( [street] => Puijonkatu 23 [postalCode] => 70100 [city] => Kuopio [country] => FI [coordinate] => Array ( [latitude] => 62.893268049685 [longitude] => 27.68189435171 ) ) [name] => Kauppakeskus Sektori [directions] => Budbee Box sijaitsee Kauppakeskus Sektorin liukuportaiden vieressä. [openingHours] => Array ( [periods] => Array ( [0] => Array ( [open] => Array ( [day] => MONDAY [time] => 08:00 ) [close] => Array ( [day] => MONDAY [time] => 18:00 ) ) [1] => Array ( [open] => Array ( [day] => TUESDAY [time] => 08:00 ) [close] => Array ( [day] => TUESDAY [time] => 18:00 ) ) [2] => Array ( [open] => Array ( [day] => WEDNESDAY [time] => 08:00 ) [close] => Array ( [day] => WEDNESDAY [time] => 18:00 ) ) [3] => Array ( [open] => Array ( [day] => THURSDAY [time] => 08:00 ) [close] => Array ( [day] => THURSDAY [time] => 18:00 ) ) [4] => Array ( [open] => Array ( [day] => FRIDAY [time] => 08:00 ) [close] => Array ( [day] => FRIDAY [time] => 18:00 ) ) [5] => Array ( [open] => Array ( [day] => SATURDAY [time] => 09:00 ) [close] => Array ( [day] => SATURDAY [time] => 15:00 ) ) ) [weekdayText] => Array ( [0] => maanantaina: 08 – 18 [1] => tiistaina: 08 – 18 [2] => keskiviikkona: 08 – 18 [3] => torstaina: 08 – 18 [4] => perjantaina: 08 – 18 [5] => lauantaina: 09 – 15 [6] => sunnuntaina: Closed ) ) [estimatedDelivery] => 2022-03-09T12:30:00.000Z [cutoff] => 2022-03-06T17:18:52.665Z [distance] => 3877 [label] => Kauppakeskus Sektori (9.3.2022 ~14:30) ) [1] => Array ( [id] => BOX2736 [address] => Array ( [street] => Särkiniementie 1 [postalCode] => 70700 [city] => Kuopio [country] => FI [coordinate] => Array ( [latitude] => 62.880945998267 [longitude] => 27.634710015346 ) ) [name] => Kuopio Neste K Särkiniemi [directions] => Budbee Box sijaitsee liikkeen sisällä juomahyllyjä vastapäätä. [openingHours] => Array ( [periods] => Array ( [0] => Array ( [open] => Array ( [day] => MONDAY [time] => 06:30 ) [close] => Array ( [day] => MONDAY [time] => 21:00 ) ) [1] => Array ( [open] => Array ( [day] => TUESDAY [time] => 06:30 ) [close] => Array ( [day] => TUESDAY [time] => 21:00 ) ) [2] => Array ( [open] => Array ( [day] => WEDNESDAY [time] => 06:30 ) [close] => Array ( [day] => WEDNESDAY [time] => 21:00 ) ) [3] => Array ( [open] => Array ( [day] => THURSDAY [time] => 06:30 ) [close] => Array ( [day] => THURSDAY [time] => 21:00 ) ) [4] => Array ( [open] => Array ( [day] => FRIDAY [time] => 06:30 ) [close] => Array ( [day] => FRIDAY [time] => 21:00 ) ) [5] => Array ( [open] => Array ( [day] => SATURDAY [time] => 08:00 ) [close] => Array ( [day] => SATURDAY [time] => 21:00 ) ) [6] => Array ( [open] => Array ( [day] => SUNDAY [time] => 09:00 ) [close] => Array ( [day] => SUNDAY [time] => 21:00 ) ) ) [weekdayText] => Array ( [0] => maanantaina: 06 – 21 [1] => tiistaina: 06 – 21 [2] => keskiviikkona: 06 – 21 [3] => torstaina: 06 – 21 [4] => perjantaina: 06 – 21 [5] => lauantaina: 08 – 21 [6] => sunnuntaina: 09 – 21 ) ) [estimatedDelivery] => 2022-03-09T12:00:00.000Z [cutoff] => 2022-03-06T17:18:52.673Z [distance] => 5758 [label] => Kuopio Neste K Särkiniemi (9.3.2022 ~14:00) ) [2] => Array ( [id] => BOX2755 [address] => Array ( [street] => Savilahdentie 10 [postalCode] => 70700 [city] => Kuopio [country] => FI [coordinate] => Array ( [latitude] => 62.887744149582 [longitude] => 27.634239326918 ) ) [name] => Prisma Kuopio [directions] => Löydät Budbee Boxin myymälän vasemman sisäänkäynnin läheisyydestä, leipomon jälkeiseltä automaattialueelta. Huomaa ettei myymälän muut kaikki sisäänkäynnit ole avoinna 24/7. [openingHours] => Array ( [periods] => Array ( [0] => Array ( [open] => Array ( [day] => MONDAY [time] => 00:00 ) [close] => Array ( [day] => MONDAY [time] => 23:30 ) ) [1] => Array ( [open] => Array ( [day] => TUESDAY [time] => 00:00 ) [close] => Array ( [day] => TUESDAY [time] => 23:30 ) ) [2] => Array ( [open] => Array ( [day] => WEDNESDAY [time] => 00:00 ) [close] => Array ( [day] => WEDNESDAY [time] => 23:30 ) ) [3] => Array ( [open] => Array ( [day] => THURSDAY [time] => 00:00 ) [close] => Array ( [day] => THURSDAY [time] => 23:30 ) ) [4] => Array ( [open] => Array ( [day] => FRIDAY [time] => 00:00 ) [close] => Array ( [day] => FRIDAY [time] => 23:30 ) ) [5] => Array ( [open] => Array ( [day] => SATURDAY [time] => 00:00 ) [close] => Array ( [day] => SATURDAY [time] => 23:30 ) ) [6] => Array ( [open] => Array ( [day] => SUNDAY [time] => 00:00 ) [close] => Array ( [day] => SUNDAY [time] => 23:30 ) ) ) [weekdayText] => Array ( [0] => maanantaina: 00 – 23 [1] => tiistaina: 00 – 23 [2] => keskiviikkona: 00 – 23 [3] => torstaina: 00 – 23 [4] => perjantaina: 00 – 23 [5] => lauantaina: 00 – 23 [6] => sunnuntaina: 00 – 23 ) ) [estimatedDelivery] => 2022-03-09T12:30:00.000Z [cutoff] => 2022-03-06T17:18:52.683Z [distance] => 5909 [label] => Prisma Kuopio (9.3.2022 ~14:30) ) [3] => Array ( [id] => BOX2758 [address] => Array ( [street] => Pirtinkaari 28 [postalCode] => 70820 [city] => Kuopio [country] => FI [coordinate] => Array ( [latitude] => 62.828854344746 [longitude] => 27.666087775005 ) ) [name] => K-Market Pirtti Kuopio [directions] => Budbee Box sijaitsee K-Market Pirtin kassojen vieressä. [openingHours] => Array ( [periods] => Array ( [0] => Array ( [open] => Array ( [day] => MONDAY [time] => 08:00 ) [close] => Array ( [day] => MONDAY [time] => 21:00 ) ) [1] => Array ( [open] => Array ( [day] => TUESDAY [time] => 08:00 ) [close] => Array ( [day] => TUESDAY [time] => 21:00 ) ) [2] => Array ( [open] => Array ( [day] => WEDNESDAY [time] => 08:00 ) [close] => Array ( [day] => WEDNESDAY [time] => 21:00 ) ) [3] => Array ( [open] => Array ( [day] => THURSDAY [time] => 08:00 ) [close] => Array ( [day] => THURSDAY [time] => 21:00 ) ) [4] => Array ( [open] => Array ( [day] => FRIDAY [time] => 08:00 ) [close] => Array ( [day] => FRIDAY [time] => 21:00 ) ) [5] => Array ( [open] => Array ( [day] => SATURDAY [time] => 09:00 ) [close] => Array ( [day] => SATURDAY [time] => 21:00 ) ) [6] => Array ( [open] => Array ( [day] => SUNDAY [time] => 11:00 ) [close] => Array ( [day] => SUNDAY [time] => 20:00 ) ) ) [weekdayText] => Array ( [0] => maanantaina: 08 – 21 [1] => tiistaina: 08 – 21 [2] => keskiviikkona: 08 – 21 [3] => torstaina: 08 – 21 [4] => perjantaina: 08 – 21 [5] => lauantaina: 09 – 21 [6] => sunnuntaina: 11 – 20 ) ) [estimatedDelivery] => 2022-03-09T13:30:00.000Z [cutoff] => 2022-03-06T17:18:52.692Z [distance] => 6631 [label] => K-Market Pirtti Kuopio (9.3.2022 ~15:30) ) [4] => Array ( [id] => BOX2698 [address] => Array ( [street] => Matkuksentie 60 [postalCode] => 70800 [city] => Kuopio [country] => FI [coordinate] => Array ( [latitude] => 62.823111188762 [longitude] => 27.604285187022 ) ) [name] => H&M Matkus Kuopio [directions] => Budbee Box sijaitsee kauppakeskus Matkuksen H&M-myymälässä. Löydät Boxin naistenosastolta, liukuportaiden vierestä myymälän pohjakerroksessa. [openingHours] => Array ( [periods] => Array ( [0] => Array ( [open] => Array ( [day] => MONDAY [time] => 10:00 ) [close] => Array ( [day] => MONDAY [time] => 20:00 ) ) [1] => Array ( [open] => Array ( [day] => TUESDAY [time] => 10:00 ) [close] => Array ( [day] => TUESDAY [time] => 20:00 ) ) [2] => Array ( [open] => Array ( [day] => WEDNESDAY [time] => 10:00 ) [close] => Array ( [day] => WEDNESDAY [time] => 20:00 ) ) [3] => Array ( [open] => Array ( [day] => THURSDAY [time] => 10:00 ) [close] => Array ( [day] => THURSDAY [time] => 20:00 ) ) [4] => Array ( [open] => Array ( [day] => FRIDAY [time] => 10:00 ) [close] => Array ( [day] => FRIDAY [time] => 20:00 ) ) [5] => Array ( [open] => Array ( [day] => SATURDAY [time] => 10:00 ) [close] => Array ( [day] => SATURDAY [time] => 19:00 ) ) [6] => Array ( [open] => Array ( [day] => SUNDAY [time] => 11:00 ) [close] => Array ( [day] => SUNDAY [time] => 18:00 ) ) ) [weekdayText] => Array ( [0] => maanantaina: 10 – 20 [1] => tiistaina: 10 – 20 [2] => keskiviikkona: 10 – 20 [3] => torstaina: 10 – 20 [4] => perjantaina: 10 – 20 [5] => lauantaina: 10 – 19 [6] => sunnuntaina: 11 – 18 ) ) [estimatedDelivery] => 2022-03-09T13:00:00.000Z [cutoff] => 2022-03-06T17:18:52.700Z [distance] => 9316 [label] => H&M Matkus Kuopio (9.3.2022 ~15:00) ) ) )
Basically I know I need a nested foreach loop for street, but why does the id not return anything?
I can see that this code results in five separate option blocks but they are all empty (even the ID).
<option value="">
<!-- -->
</option>
<option value="">
<!-- -->
</option>
<option value="">
<!-- -->
</option>
<option value="">
<!-- -->
</option>
<option value="">
<!-- -->
</option>
What am I doing wrong with this array?

How to list a multidimensional array in chronological order

I have this array of professionals, locations, dates and times. How can I list these dates and times within it. And at the same time, if a certain time was chosen, I need to know the date, local_id, and the professional_id it belongs to.
I tried foreach nested and saving each information for each iteration, but in the end it didn't work because I need to display the dates in chronological order.
I've never worked with an array like this, I'm pretty lost.
Array
(
[profissional_id] => Array
(
[74] => Array
(
[local_id] => Array
(
[0] => Array
(
[2021-11-09] => Array
(
)
[2021-11-16] => Array
(
[0] => 08:00:00
[1] => 08:40:00
[2] => 09:20:00
[3] => 10:40:00
[4] => 12:00:00
)
[2021-11-23] => Array
(
[0] => 08:00:00
[1] => 08:40:00
[2] => 09:20:00
[3] => 10:00:00
[4] => 10:40:00
[5] => 11:20:00
[6] => 12:00:00
)
[2021-11-30] => Array
(
[0] => 08:00:00
[1] => 08:40:00
[2] => 09:20:00
[3] => 10:00:00
[4] => 10:40:00
[5] => 11:20:00
[6] => 12:00:00
)
[2021-12-07] => Array
(
[0] => 08:00:00
[1] => 08:40:00
[2] => 09:20:00
[3] => 10:00:00
[4] => 10:40:00
[5] => 11:20:00
[6] => 12:00:00
)
)
[5] => Array
(
[2021-11-09] => Array
(
[0] =>
)
[2021-11-16] => Array
(
[0] =>
)
[2021-11-23] => Array
(
[0] =>
)
[2021-11-30] => Array
(
[0] =>
)
[2021-12-07] => Array
(
[0] =>
)
)
)
)
[33] => Array
(
[local_id] => Array
(
[3] => Array
(
[2021-11-13] => Array
(
[0] => 08:00:00
[1] => 08:30:00
[2] => 09:00:00
[3] => 09:30:00
[4] => 10:00:00
[5] => 10:30:00
[6] => 11:00:00
[7] => 11:30:00
[8] => 12:00:00
)
[2021-11-20] => Array
(
)
[2021-11-27] => Array
(
[0] => 08:30:00
[1] => 09:00:00
[2] => 09:30:00
[3] => 10:00:00
[4] => 10:30:00
[5] => 11:00:00
[6] => 11:30:00
[7] => 12:00:00
)
[2021-12-04] => Array
(
[0] => 08:00:00
[1] => 08:30:00
[2] => 09:30:00
[3] => 10:00:00
[4] => 10:30:00
[5] => 11:00:00
[6] => 11:30:00
[7] => 12:00:00
)
)
)
)
)
)
This is an example of an array type return I get

Unset element in array nested, between a time interval

I'm trying to remove an element.
I've a reservation with:
$StartTime = "14:00"; $EndTime = "16:30";
and Array with
Array
(
[Tennis 1] => Array
(
[IntervalTime] => Array
(
[Lun] => Array
(
[0] => 10:00
[1] => 11:00
[2] => 12:00
[3] => 13:00
[4] => 14:00
[5] => 15:00
[6] => 16:00
[7] => 17:00
[8] => 18:00
[9] => 19:00
[10] => 20:00
)
[Mar] => Array
(
[0] => 12:00
[1] => 13:00
[2] => 14:00
[3] => 15:00
)
[Mer] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
)
[Gio] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
)
[Ven] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
)
[Sab] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
[5] => 14:00
[6] => 15:00
[7] => 16:00
[8] => 17:00
[9] => 18:00
)
[Sun] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
[5] => 14:00
[6] => 15:00
[7] => 16:00
[8] => 17:00
[9] => 18:00
)
)
)
I need to remove element between $StartTime and $EndTime
so for "Lun" array need to be removed:
[0] => 10:00
[1] => 11:00
[2] => 12:00
[3] => 13:00
[4] => 14:00 // need remove
[5] => 15:00 // need remove
[6] => 16:00 // need remove
[7] => 17:00
[8] => 18:00
[9] => 19:00
[10] => 20:00
I would to foearch the time from Start to End date, and then remove with:
unset($DisponibilitaRoom[$Nomeroom]["IntervalTime"]["Lun"][$index]);
You correct - simple unset should do it - but you need to loop on the times and remove them as:
foreach($arr['Tennis 1']['IntervalTime']['Lun'] as $k => $v) {
if ($v >= $StartTime && $v < $EndTime)
unset($arr['Tennis 1']['IntervalTime']['Lun'][$k]);
}
Live example: 3v4l
Or you can use array-filter as
$arr['Tennis 1']['IntervalTime']['Lun'] = array_filter($arr['Tennis 1']['IntervalTime']['Lun'], function ($v) use ($StartTime, $EndTime) {
return !($v >= $StartTime && $v < $EndTime);
} );
You can use array_walk_recursive with array_map and array_filter
array_walk_recursive($a['Tennis 1']['IntervalTime'], function(&$v) use ($StartTime, $EndTime){
if($v >= $StartTime && $v <= $EndTime) $v = false;
});
$f['Tennis 1']['IntervalTime'] = array_map('array_filter', $a['Tennis 1']['IntervalTime']);
print_r($f);
Working example :- https://3v4l.org/4tNEM

How to delete keys from multi-dimension array dynamically

I have to filter this array to see only future items.
How do I unset items from which the timeEnd has expired?
i.e when I call this array at 17:00 there's only array[2] left.
Array
(
[0] => Array
(
[id] => 3034
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 09:30
[description] => User_A
[locationId] => 1
[roomId] => 8
[relationId] => 104
)
[1] => Array
(
[id] => 2524
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 12:00
[description] => User_B
[locationId] => 1
[roomId] => 5
[relationId] => 86
)
[2] => Array
(
[id] => 2533
[date] => 28-09-2016
[timeStart] => 09:00
[timeEnd] => 18:00
[description] => User_C
[locationId] => 1
[roomId] => 4
[relationId] => 31
)
)
foreach ($reservations as $key=>$reservation) {
$expireDate = $reservation['date'].' '.$reservation['timeEnd'];
if (strtotime($expireDate) <= strtotime('now')){
unset($reservations[$key]);
}
}

Restructuring PHP array

I have tried many different variation, but cannot get the right structure. Maybe, you expert might what to give it a try.
What I need to do is return the therapist who may have multiple children assigned to him. It will return the children's schedules that are assigned to him.
I have tried the following Code. This is the closest I can get to the format I need.
// Grab the therapist
$therapist = $this->Therapist->find('first', array('conditions' => array('Therapist.' . $this->Therapist->primaryKey => $id)));
// Grab the child(ren) assigned to the therapist
// Returns id as key and name as value.
$children = $this->Child->get_children($id);
$schedule = array();
// Loop through the assigned children and get the id (key).
foreach($children as $key => $value):
// Loop through and grab all the scheduled days and times for child(ren).
foreach($this->Schedule->get_full_schedule($key) as $child):
// Have the child name at the top of the array.
if($name != $child['Child']['child_name']):
$schedule[] = array_push($schedule, array('child_name' => $child['Child']['child_name']));
$name = $child['Child']['child_name'];
endif;
// Get all the scheduled days for the child and add to array.
if($child['Schedule']['child_id'] == $child['Child']['id']):
$schedule[]['Schedule'] = $child['Schedule'];
endif;
endforeach;
endforeach;
Which outputs the following Array:
Array
(
[0] => Array
(
[child_name] => John Smith
)
[1] => 1
[2] => Array
(
[Schedule] => Array
(
[id] => 19
[child_id] => 197
[days] => Monday
[start_time] => 17:00:00
[end_time] => 22:00:00
)
)
[3] => Array
(
[child_name] => Jane Smith
)
[4] => 4
[5] => Array
(
[Schedule] => Array
(
[id] => 16
[child_id] => 138
[days] => Monday
[start_time] => 09:00:00
[end_time] => 17:00:00
)
)
[6] => Array
(
[Schedule] => Array
(
[id] => 17
[child_id] => 138
[days] => Sunday
[start_time] => 09:00:00
[end_time] => 12:00:00
)
)
[7] => Array
(
[Schedule] => Array
(
[id] => 18
[child_id] => 138
[days] => Tuesday
[start_time] => 09:00:00
[end_time] => 17:00:00
)
)
)
What I would like is:
Array
(
[0] => Array
(
[child_name] => John Smith
[0] => Array
(
[Schedule] => Array
(
[id] => 19
[child_id] => 197
[days] => Monday
[start_time] => 17:00:00
[end_time] => 22:00:00
)
)
)
[1] => Array
(
[child_name] => Jane Smith
[0] => Array
(
[Schedule] => Array
(
[id] => 16
[child_id] => 138
[days] => Monday
[start_time] => 09:00:00
[end_time] => 17:00:00
)
)
[1] => Array
(
[Schedule] => Array
(
[id] => 17
[child_id] => 138
[days] => Sunday
[start_time] => 09:00:00
[end_time] => 12:00:00
)
)
[2] => Array
(
[Schedule] => Array
(
[id] => 18
[child_id] => 138
[days] => Tuesday
[start_time] => 09:00:00
[end_time] => 17:00:00
)
)
)
Any help is appreciated.
Thank,
Greg
Have you thought about just using Containable Behavior? Seems MUCH easier than the way you're trying to do it:
$this->Therapist->find('first', array(
'conditions' => array(
'Therapist.' . $this->Therapist->primaryKey => $id
),
'contain' => array(
'Child' => array(
'Schedule'
)
)
));
Not only is it a lot easier, but your data should come back in an acceptable and nested format.

Categories