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
Related
I want to convert below array.
Criteria is,
If projectId same then store same project id data under array of project which contains projectId as key.
Array
(
[0] => Array
(
[PMST] => Array
(
[id] => 4
[project_id] => 25
[task_name] => Final task 3
[start_date] => 2016-06-21 00:00:00
[end_date] => 2016-06-29 00:00:00
)
[PMSP] => Array
(
[id] => 25
[project_name] => Project 3
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
)
[1] => Array
(
[PMST] => Array
(
[id] => 9
[project_id] => 28
[task_name] => Task Test 333 edit
[start_date] => 2016-06-19 00:00:00
[end_date] => 2016-06-29 00:00:00
)
[PMSP] => Array
(
[id] => 28
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
)
[2] => Array
(
[PMST] => Array
(
[id] => 1
[project_id] => 28
[task_name] => Task 1
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
[PMSP] => Array
(
[id] => 28
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
)
)
Desire Output
Array
(
[25] => Array
(
[PMSP] => Array
(
[id] => 25
[company_id] => 1114701
[project_name] => Project 3
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
[taskdetails] => Array
(
[0] => Array(
[PMST] => Array
(
[id] => 4
[project_id] => 25
[company_id] => 1114701
[task_name] => Final task 3
[start_date] => 2016-06-21 00:00:00
[end_date] => 2016-06-29 00:00:00
)
)
)
)
[28] => Array
(
[PMSP] => Array
(
[id] => 28
[company_id] => 1114701
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
[taskdetails] => Array
(
[0] => Array
(
[PMST] => Array
(
[id] => 9
[project_id] => 28
[company_id] => 1114701
[task_name] => Task Test 333 edit
[start_date] => 2016-06-19 00:00:00
[end_date] => 2016-06-29 00:00:00
)
)
[1] => Array(
[PMST] => Array
(
[id] => 1
[project_id] => 28
[company_id] => 1114701
[task_name] => Task 1
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
)
)
)
)
Loop over your input array using foreach, adding the PMST data to an output array as you go. It looks like it's safe to assume the PMSP data for two tasks on the same project will be the same.
$output = [];
foreach ($input as $task) {
if (!isset($output[$task["PMSP"]["id"]])) {
$output[$task["PMSP"]["id"]] = ["PMSP" => $task["PMSP"], "taskdetails" => []];
}
$output[$task["PMSP"]["id"]]["taskdetails"][] = $task["PMST"];
}
See the below example
Example
$data = [];
foreach ($a as $b) {
$key = $b["PMSP"]["id"];
if (!isset($data[$key])) {
$data[$key] = ["PMSP" => $b["PMSP"], "taskdetails" => []];
}
$data[$key]["taskdetails"][] = $b["PMST"];
}
echo "<pre>";
print_r($data);exit();
Your latest data
Example 2
I am new to PHP multi-dimentional arrays, So I need 100% working answer please...
I had tried it by using muli-sort and usort function but they are not working for me, honestly i didn't know how to use them in case of multi-dimentional arrays!
In my case (how I am implementing multi-sort), it is only sorting the first day's array
Array (
[0] => Array (
[0] => Array (
[0] => Cycling
[1] => 03:30 PM-04:00 PM
[2] => Criterium International
[3] => 0.5
[4] => SN1
[5] => 100
[6] => 1459524600
)
[1] => Array (
[0] => Rugby
[1] => 03:00 PM-05:15 PM
[2] => Super League Rugby - Wolves v Warriors
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459522800
)
[2] => Array (
[0] => Rugby
[1] => 05:00am-07:30 AM
[2] => National Rugby League Titans vs Broncos
[3] => 2.5
[4] => SNWorld
[5] => 100
[6] => 1459485000
)
)
[1] => Array (
[0] => Array (
[0] => Rugby
[1] => 09:45 AM-12:00 PM
[2] => Super League Rugby - Red Devils v E
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459590300
)
[1] => Array (
[0] => Rugby
[1] => 12:00 PM-02:00 PM
[2] => Super League Rugby - Vikings v Dragons
[3] => 2
[4] => SNOverflow
[5] => 100
[6] => 1459598400
)
[2] => Array (
[0] => BPL Soccer
[1] => 07:30 AM-09:45 AM
[2] => Aston Villa v Chelsea
[3] => 2.25
[4] => SNRegions
[5] => 100
[6] => 1459582200
)
)
[2] => Array (
[0] => Array (
[0] => The Wheel Highlights
[1] => 11:00 PM-12:30 AM
[2] =>
[3] => 1.5
[4] => Sportsnet 360
[5] => 100
[6] => 1459724400
)
[1] => Array (
[0] => The Wheel Highlights
[1] => 10:00 PM-11:00 PM
[2] =>
[3] => 1
[4] => Sportsnet 360
[5] => 100
[6] => 1459720800
)
[2] => Array (
[0] => CHL Hockey
[1] => 06:00 PM-09:00 PM
[2] => Brandon # Edmonton
[3] => 3
[4] => Sportsnet 360
[5] => 100
[6] => 1459706400
)
)
)
My desired output is like this:
Array (
[0] => Array (
[0] => Array (
[0] => Rugby
[1] => 05:00am-07:30 AM
[2] => National Rugby League Titans vs Broncos
[3] => 2.5
[4] => SNWorld
[5] => 100
[6] => 1459485000
)
[1] => Array (
[0] => Rugby
[1] => 03:00 PM-05:15 PM
[2] => Super League Rugby - Wolves v Warriors
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459522800
)
[2] => Array (
[0] => Cycling
[1] => 03:30 PM-04:00 PM
[2] => Criterium International
[3] => 0.5
[4] => SN1
[5] => 100
[6] => 1459524600
)
)
[1] => Array (
[0] => Array (
[0] => BPL Soccer
[1] => 07:30 AM-09:45 AM
[2] => Aston Villa v Chelsea
[3] => 2.25
[4] => SNRegions
[5] => 100
[6] => 1459582200
)
[1] => Array (
[0] => Rugby
[1] => 09:45 AM-12:00 PM
[2] => Super League Rugby - Red Devils v E
[3] => 2.25
[4] => SNOverflow
[5] => 100
[6] => 1459590300
)
[2] => Array (
[0] => Rugby
[1] => 12:00 PM-02:00 PM
[2] => Super League Rugby - Vikings v Dragons
[3] => 2
[4] => SNOverflow
[5] => 100
[6] => 1459598400
)
)
[2] => Array (
[0] => Array (
[0] => CHL Hockey
[1] => 06:00 PM-09:00 PM
[2] => Brandon # Edmonton
[3] => 3
[4] => Sportsnet 360
[5] => 100
[6] => 1459706400
)
[1] => Array (
[0] => The Wheel Highlights
[1] => 10:00 PM-11:00 PM
[2] =>
[3] => 1
[4] => Sportsnet 360
[5] => 100
[6] => 1459720800
)
[2] => Array (
[0] => The Wheel Highlights
[1] => 11:00 PM-12:30 AM
[2] =>
[3] => 1.5
[4] => Sportsnet 360
[5] => 100
[6] => 1459724400
)
)
)
Probably your code has failed due to multi levels array. To sort items of each first level you have to perform a foreach on the first level and process it by reference, otherwise the sort result doesn't affect main array:
# ↓ “&” means “by reference”
foreach( $array as &$sub )
{
usort ( $sub, function( $a, $b ) { return $a[6] - $b[6]; } );
}
eval.in demo
I'm having the following array of dates :
Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
[9] => 2016-02-13
[10] => 2016-02-14
[11] => 2016-02-15
[12] => 2016-03-13
[13] => 2016-03-14
[14] => 2016-03-15
[15] => 2016-03-16
[16] => 2016-03-17
[17] => 2016-03-18
[18] => 2016-03-19
[19] => 2016-04-19
[20] => 2016-04-20
[21] => 2016-04-21
[22] => 2016-04-22
)
How can i get the array dates that form a range. Like
Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
)
Array
(
[0] => 2016-02-13
[1] => 2016-02-14
[2] => 2016-02-15
)
Array
(
[0] => 2016-03-13
[1] => 2016-03-14
[2] => 2016-03-15
[3] => 2016-03-16
[4] => 2016-03-17
[5] => 2016-03-18
[6] => 2016-03-19
)
The dates in the main array I listed can be of any order. The example I provided all the date ranges can be identifiable. But the raay is dynamic and can have date in random order. All I want to have date ranges out if that array. I'm getting mad of thinking. Please help. Thanks in advance.
You have the following code. Please try this.
$array = Array(0 => '2016-02-01',
1 => '2016-02-02',
2 => '2016-03-19',
3 => '2016-02-04',
4 => '2016-02-05',
5 => '2016-03-18',
6 => '2016-02-07',
7 => '2016-02-08',
8 => '2016-02-09',
9 => '2016-02-13',
10 => '2016-02-14',
11 => '2016-02-15',
12 => '2016-03-13',
13 => '2016-03-14',
14 => '2016-03-15',
15 => '2016-03-16',
16 => '2016-03-17',
17 => '2016-02-06',
18 => '2016-02-03',
19 => '2016-04-19',
20 => '2016-04-20',
21 => '2016-04-21',
22 => '2016-04-22');
//echo "<pre>";print_r($array);
sort($array);
$newDateArray = array();
$i = 0;
$arrayCount = count($array);
foreach($array as $key=>$val) {
//echo $key;
if(isset($array[$key+1])) {
$date1 = $val;
$date2 = $array[$key+1];
$newDateArray[$i][] = $date1;
$datetime1 = date_create($date1);
$datetime2 = date_create($date2);
$interval = date_diff($datetime1, $datetime2);
if($interval->days > 1) {
$i++;
}
}
else if($arrayCount == ($key+1)) {
$newDateArray[$i][] = $date2;
}
}
echo "<pre>";print_r($newDateArray);exit;
your result would be as follows.
Array
(
[0] => Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
)
[1] => Array
(
[0] => 2016-02-13
[1] => 2016-02-14
[2] => 2016-02-15
)
[2] => Array
(
[0] => 2016-03-13
[1] => 2016-03-14
[2] => 2016-03-15
[3] => 2016-03-16
[4] => 2016-03-17
[5] => 2016-03-18
[6] => 2016-03-19
)
[3] => Array
(
[0] => 2016-04-19
[1] => 2016-04-20
[2] => 2016-04-21
[3] => 2016-04-22
)
)
Now you can easily split your array. and get the result what you exactly want.
Have a Great Day!!!... Fill free to ask more your doubts...
I am try to loop through the following stdClass Object array.
Array
(
[0] => stdClass Object
(
[key] => 49
[values] => Array
(
[0] => Kansas City Chiefs
[1] => -3
[2] => Denver Broncos
[3] => 3
[4] => 41
[5] => 2015-09-18 00:25:00
)
)
[1] => stdClass Object
(
[key] => 50
[values] => Array
(
[0] => Carolina Panthers
[1] => -3.5
[2] => Houston Texans
[3] => 3.5
[4] => 40
[5] => 2015-09-20 17:00:00
)
)
[2] => stdClass Object
(
[key] => 51
[values] => Array
(
[0] => New Orleans Saints
[1] => -10
[2] => Tampa Bay Buccaneers
[3] => 10
[4] => 47
[5] => 2015-09-20 17:00:00
)
)
[3] => stdClass Object
(
[key] => 52
[values] => Array
(
[0] => Pittsburgh Steelers
[1] => -6
[2] => San Francisco 49ers
[3] => 6
[4] => 45
[5] => 2015-09-20 17:00:00
)
)
[4] => stdClass Object
(
[key] => 53
[values] => Array
(
[0] => Minnesota Vikings
[1] => -3
[2] => Detroit Lions
[3] => 3
[4] => 43
[5] => 2015-09-20 17:00:00
)
)
[5] => stdClass Object
(
[key] => 54
[values] => Array
(
[0] => Buffalo Bills
[1] => 1
[2] => New England Patriots
[3] => -1
[4] => 45
[5] => 2015-09-20 17:00:00
)
)
[6] => stdClass Object
(
[key] => 55
[values] => Array
(
[0] => Chicago Bears
[1] => 2
[2] => Arizona Cardinals
[3] => -2
[4] => 45
[5] => 2015-09-20 17:00:00
)
)
[7] => stdClass Object
(
[key] => 56
[values] => Array
(
[0] => Cleveland Browns
[1] => 1
[2] => Tennessee Titans
[3] => -1
[4] => 41
[5] => 2015-09-20 17:00:00
)
)
[8] => stdClass Object
(
[key] => 57
[values] => Array
(
[0] => Cincinnati Bengals
[1] => -3.5
[2] => San Diego Chargers
[3] => 3.5
[4] => 46
[5] => 2015-09-20 17:00:00
)
)
[9] => stdClass Object
(
[key] => 58
[values] => Array
(
[0] => Washington Redskins
[1] => 3.5
[2] => St. Louis Rams
[3] => -3.5
[4] => 41
[5] => 2015-09-20 17:00:00
)
)
[10] => stdClass Object
(
[key] => 59
[values] => Array
(
[0] => New York Giants
[1] => -2.5
[2] => Atlanta Falcons
[3] => 2.5
[4] => 51
[5] => 2015-09-20 17:00:00
)
)
[11] => stdClass Object
(
[key] => 60
[values] => Array
(
[0] => Oakland Raiders
[1] => 6
[2] => Baltimore Ravens
[3] => -6
[4] => 43
[5] => 2015-09-20 20:05:00
)
)
[12] => stdClass Object
(
[key] => 61
[values] => Array
(
[0] => Jacksonville Jaguars
[1] => 6
[2] => Miami Dolphins
[3] => -6
[4] => 41
[5] => 2015-09-20 20:05:00
)
)
[13] => stdClass Object
(
[key] => 62
[values] => Array
(
[0] => Philadelphia Eagles
[1] => -5
[2] => Dallas Cowboys
[3] => 5
[4] => 55
[5] => 2015-09-20 20:25:00
)
)
[14] => stdClass Object
(
[key] => 63
[values] => Array
(
[0] => Green Bay Packers
[1] => -3.5
[2] => Seattle Seahawks
[3] => 3.5
[4] => 49
[5] => 2015-09-21 00:30:00
)
)
[15] => stdClass Object
(
[key] => 64
[values] => Array
(
[0] => Indianapolis Colts
[1] => -7
[2] => New York Jets
[3] => 7
[4] => 47
[5] => 2015-09-22 00:30:00
)
)
)
When I use the following syntax it prints one line correctly and one line blank
foreach($tableData as $obj)
{
foreach ($obj as $tr)
{
echo '<tr><td>'.$mysqldate = date( 'g:ia - D', $phpdate = strtotime($tr[5])- 60 * 60 * 5).'</td><td><strong>'.($i += 4).'</strong> '.$tr[0].'<br><br><strong>'.($iii += 4).'</strong> Over</td><td>'.$tr[1].'<br><br>'.$tr[4].'</td><td><strong>'.($ii += 4).'</strong> '.$tr[2].'<br><br><strong>'.($iiii += 4).'</strong> Under</td><td>'.$tr[3].'<br><br>'.$tr[4].'</td></tr>';
}
}
My guess is that its looping through the key, when is where it comes up blank, than it goes through the values where i get the results I want. I've tried a few things that does seem to work. How do I loop through avoiding the [key]?
You need to access the object's values property instead, like this:
foreach($tableData as $obj)
{
$tr = $obj->values;
echo ....
}
Try this:
foreach($tableData as $key => $val){
$val2 = $val->values;
echo $val[0];
}
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.