I am working on a statistics application and I want to output the amount of interactions that happened by day.
I have an multidimensional array that pulls all the information from the database, here is an example:
[0] => Array
(
[date] => 2012-07-26
[location] => 709c6d241674ca22
[action] => start_scan
)
[1] => Array
(
[date] => 2012-07-26
[location] => 709c6d241674ca22
[action] => scan_displayed
)
[2] => Array
(
[date] => 2012-07-27
[location] => 709c6d241674ca22
[action] => lower_device
)
[3] => Array
(
[date] => 2012-07-27
[location] => 709c6d241674ca22
[action] => how_to_use_displayed
)
[4] => Array
(
[date] => 2012-07-27
[location] => 709c6d241674ca22
[action] => raise_device
)
[5] => Array
(
[date] => 2012-07-28
[location] => 709c6d241674ca22
[action] => scan_displayed
)
I can work out what day each interaction occurred on by formatting the date:
date('D', strtotime('2012-07-26'));
My question is how do I count how many interactions happened on each day of the week and then output it, something like:
[Sunday] => 2
[Monday] => 3
[Tueday] => 1
[Wednesday] => 5
[Thursday] => 10
[Friday] => 4
[Saturday] => 9
Any suggestions are really appreciated!
$dates = array();
foreach($data as $item) {
$day = date('l', $item['date']);
$dates[$day]++;
}
var_dump($dates);
You may want to declare the array as $dates = array('Monday'=>0, 'Tuesday'=>0 .... ) to get the array containing the days with no interactions.
Related
I have two multidimensional associative arrays with differen items count.
Important is that I don't know which aray will have more elements (A or B)
First array (A):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[0] => 10887
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 13184
[1] => 10707
[2] => 8350
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Second array (B):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
After compare these arrays (A, B) I'd like to retrive something like that:
Array A(remove productID if exists in array B):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 10707
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Array B(no changes):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Populate a flat "blacklist" array from your second array.
Loop over the first array's rows and filter the productId values against the blacklist array.
Code: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
var_export(
array_map(
fn($row) => array_replace($row, ['productID' => array_values(array_diff($row['productID'], $blacklist))]),
$a
)
);
Or if you don't mind a classic foreach(), then this may be easier to read: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
foreach ($a as &$row) {
$row['productID'] = array_values(array_diff($row['productID'], $blacklist));
}
var_export($a);
I have following multidimensional array. I am not getting suitable title for my question. You are free to edit and change.
Array(
[2018-05-01] => Array
(
[0] => Array
(
[Zone] => 0
[Time] => 13:06
)
[1] => Array
(
[Zone] => 0
[Time] => 13:08
)
[2] => Array
(
[Zone] => 2
[Time] => 13:10
)
[3] => Array
(
[Zone] => 2
[Time] => 13:14
)
[4] => Array
(
[Zone] => 0
[Time] => 13:16
)
[5] => Array
(
[Zone] => 0
[Time] => 13:17
)
[6] => Array
(
[Zone] => 2
[Time] => 13:21
)
[7] => Array
(
[Zone] => 2
[Time] => 13:25
)
[8] => Array
(
[Zone] => 0
[Time] => 13:28
)
[9] => Array
(
[Zone] => 0
[Time] => 13:32
)
)
[2018-05-04] => Array
(
[0] => Array
(
[Zone] => 1
[Time] => 13:26
)
[1] => Array
(
[Zone] => 1
[Time] => 13:28
)
[2] => Array
(
[Zone] => 1
[Time] => 13:30
)
[3] => Array
(
[Zone] => 0
[Time] => 13:34
)
[4] => Array
(
[Zone] => 0
[Time] => 13:36
)
[5] => Array
(
[Zone] => 1
[Time] => 13:37
)
[6] => Array
(
[Zone] => 1
[Time] => 13:41
)
)
)
I have to manipulate above array in such a way that, Same Zone should not come simultaneously. And this depend upon the conditions -
Condition 1
If Sub array has only Zone => 0 and Zone => 2. For example array with key 2018-05-01 has only 0 and 2 as Zones.
We will only fetch First Zone => 0, last Zone => 2 , first Zone => 0 and again last Zone => 2.
Note here, if there is no Zone => 2 after Zone => 0, then in this case we fetch last Zone => 0 instead of first Zone => 0.
Please see my output for clearification.
Condition 2
If Sub array has Zone => 1. For example array with key 2018-05-04. We will fetch only First occurrence of each.
Here is the desired output
Array(
[2018-05-01] => Array
(
[0] => Array
(
[Zone] => 0
[Time] => 13:06
)
[3] => Array
(
[Zone] => 2
[Time] => 13:14
)
[4] => Array
(
[Zone] => 0
[Time] => 13:16
)
[7] => Array
(
[Zone] => 2
[Time] => 13:25
)
[9] => Array
(
[Zone] => 0
[Time] => 13:32
)
)
[2018-05-04] => Array
(
[0] => Array
(
[Zone] => 1
[Time] => 13:26
)
[3] => Array
(
[Zone] => 0
[Time] => 13:34
)
[5] => Array
(
[Zone] => 1
[Time] => 13:37
)
)
)
What I have tried is -
What I have been thinking of doing is using array_column to check if the sub array has "1" as Zone value. If yes then we will implement condition 1 otherwise Condition 2. Then loop through each subarray and comparing current iteration with previous one. But I am not getting exact view, and I m doubtful if this will be an elegant solution.
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.
What I want to do
here is the main array
Array
(
[0] => Array
(
[Culture] => Array
(
[id] => 8
[title] => test123
[description] => test123
[year] => 2012
[photo] => test123.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[1] => Array
(
[Culture] => Array
(
[id] => 9
[title] => here title
[description] => here title
[year] => 2012
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[2] => Array
(
[Culture] => Array
(
[id] => 11
[title] => here title 2
[description] => here title 2
[year] => 2012
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[3] => Array
(
[Culture] => Array
(
[id] => 12
[title] => here title 3
[description] => here title 3
[year] => 2013
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[4] => Array
(
[Culture] => Array
(
[id] => 13
[title] => here title 4
[description] => here title 4
[year] => 2014
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[5] => Array
(
[Culture] => Array
(
[id] => 14
[title] => here title 5
[description] => here title 5
[year] => 2015
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
)
now from this array I want array of year (by key)
like:
Array
(
[0]=>2012
[1]=>2013
[2]=>2014
[3]=>2015
)
Loop through your array and assign those years to a new array with their keys intact.
$years=array();
foreach($yourArray as $key=>$value)
{
$years[$key]=$value["Culture"]["year"];
}
$years = array_unique($years);
print_r($years);
You can first loop through the array with a foreach loop and then use array_unique to get the array of years:
$years = array();
foreach ($records as $record) {
$years[] = $record['Culture']['year'];
}
$years = array_unique($years);
Demo!
$years = [];
foreach($arr as $newarray){
$years[] = $newarray['Culture']['year'];
}
$years = array_unique($years);
The new array years will now hold all the years in the old array. array_unique will get rid of all duplicate years.
My way is to use array-walk that takes anonymous function to fill the array, the solution will be in only one line of code.
Its working for me
foreach($cultures as $row)
{
$year[]=$row['Culture']['year'];
}
$year = array_unique($year);
$year = array_values($year);
echo "<pre>";print_r($year);
I Have an array which looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-15 13:27:35
[status] => 3
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 6
)
)
[1] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-12 13:27:35
[status] => 1
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 4
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
[2] => Array
(
[0] =>
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
I want to "collapse" each second dimension array and obtain the max DATE value and the max status value.So the first index would return 2010-09-18 13:27:35 and '6' etc.
The problem is further complicated by the empty array in the last index. I would like to use this empty array and report it as the MAX date and status.
Thank in advance!
Thanks for looking everybody. I figured it out.
$date=array();
$status=array();
$availability=array();
foreach($set as $key => $value)
{
foreach($value as $value2)
{
if(isset($value2[1]))
{
$date[$key][]=$value2[1];
$status[$key][]=$value2[2];
}
else
{
$date[$key][]='2022-09-18 13:27:35';
$status[$key][]='0';
}
}
$availability[$key]=array(max($date[$key]),min($status[$key]));
}