I have a MySQL query that returns a dump of data like so (duplicates expected and needed for other columns that are excluded for this question):
SessionID DayofWeek SessionDetails Description
1 Sunday 1:00 PM Foo
1 Sunday 1:00 PM Foo
1 Sunday 1:00 PM Foo
1 Sunday 1:00 PM Foo
2 Monday 10:00 AM Foo
2 Monday 10:00 AM Foo
2 Monday 10:00 AM Foo
2 Monday 10:00 AM Foo
3 Monday 7:00 PM Barr
3 Monday 7:00 PM Barr
3 Monday 7:00 PM Barr
3 Monday 7:00 PM Barr
I am trying to create an array for each day and only store the different session/times once in it, e.g.,
Sunday => Array (
[SessionID] => 1
[SessionDetails] => 1:00 PM
[Description] => Foo
)
Monday=> Array (
[SessionID] => 2
[SessionDetails] => 10:00 AM
[Description] => Foo
[SessionID] => 3
[SessionDetails] => 7:00 PM
[Description] => Bar
)
I am setting a blank array for each day before my foreach ($sqlresults as $row) loop, like $sunday=array(); $monday=array(); etc.
Then inside the loop I am doing:
if (!in_array($row['SessionID'],$sunday)){
$sunday[]=array($row['SessionID'],$row['Description'], $row['SessionDetails']);
}
It sort-of-works but it's not checking the if (!in_array) part right and it's adding every single (duplicate) SessionID as a new array e.g.,
print_r($sunday)
Array
(
[0] => Array
(
[0] => 1
[1] => Foo
[2] => 1 PM
)
[1] => Array
(
[0] => 1
[1] => Foo
[2] => 1 PM
)
[2] => Array
(
[0] => 1
[1] => Foo
[2] => 1 PM
)
[3] => Array
(
[0] => 1
[1] => Barr
[2] => 1 PM
)
[4] => Array
(
[0] => 2
[1] => Barr
[2] => 10 AM
)
etc...
I tried to check the sessionID inside the array before adding to it:
if (!in_array($row['SessionID'][],$sunday)){
But "Fatal error: Cannot use [] for reading"
So I tried:
if (!in_array($sunday[$row['SessionID']],$sunday)){
But "Notice: Undefined offset: 1"
I tried setting it to associative values:
if (!in_array($row['SessionID'],$sunday)){
$sunday[]=array("SessionID"=>($row['SessionID']),"Description"=>($row['Description']), "Time"=>($row['SessionDetails']));
}
Same problem, except the array is easier to read:
[0] => Array
(
[SessionID] => 1
[Description] => Foo
[Time] => 1 PM
)
[1] => Array
(
[SessionID] => 1
[Description] => Foo
[Time] => 1 PM
)
TLDR: How can I get it to check the index [SessionID] before trying to add to the day array?
Here's a simple way to do it using array_unique.
$result = [];
foreach (array_unique($rows, SORT_REGULAR) as $row) {
$result[$row['DayofWeek']][] = $row;
}
You can run the code here.
Related
The current output is..
Array
(
[0] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[1] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
I would like the array output to be..
Array
(
[20] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[21] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
The code I am currently using to generate the array is..
$sql=mysqli_query($conn,"SELECT * FROM `live`");
/*every time it fetches the row, adds it to array...*/
while($liveuserdata[]=mysqli_fetch_array($sql, MYSQLI_ASSOC));
I can't show you what i've tried as I don't know where to begin dispite several rephrased searches :-/
It is as simple as:
$sql = mysqli_query($conn,"SELECT * FROM `live`");
$liveuserdata = [];
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
$liveuserdata[$row['ID']] = $row;
}
I have two associative arrays with time and total booking for that particular time.i need to merge this 2 arrays by adding booking count for corresponding timing.
Array 1:
Array
(
[0] => stdClass Object
(
[time] => 03:00 PM - 04:30 PM
[booked] => 3
)
[1] => stdClass Object
(
[time] => 05:00 PM - 06:30 PM
[booked] => 2
)
)
Array 2:
Array
(
[0] => stdClass Object
(
[time] => 03:00 PM - 04:30 PM
[booked] => 3
)
[1] => stdClass Object
(
[time] => 07:00 PM - 08:30 PM
[booked] => 1
)
)
This is the output i need to generate..tried many ways to merge with finding sum but i can only merge this arrays...
Array
(
[0] => stdClass Object
(
[time] => 03:00 PM - 04:30 PM
[booked] => 6
)
[1] => stdClass Object
(
[time] => 05:00 PM - 06:30 PM
[booked] => 2
)
[2] => stdClass Object
(
[time] => 07:00 PM - 08:30 PM
[booked] => 1
)
)
You can use array_merge with foreach and array_key_exists
$a3 = array_merge($a1,$a2);
$r = [];
foreach($a3 as $v){
array_key_exists($v->time, $r)
?
($r[$v->time]->booked += $v->booked)
:
($r[$v->time] = $v);
}
print_r(array_values($r)); // re-arrange the array keys
Working example : https://3v4l.org/JAZ4d
Sorry for my English ,I am new here and new to PHP.I have array like below,I was trying find count of consecutive date-time from for every user within 10 min,
Array
(
[0] => Array
(
[user] => A
[time] => 2018-08-01 14:12:00
)
[1] => Array
(
[user] => B
[time] => 2018-08-01 14:12:00
)
[2] => Array
(
[user] => A
[time] => 2018-08-01 14:13:00
)
[3] => Array
(
[user] => A
[time] => 2018-08-01 14:14:00
)
[4] => Array
(
[user] => A
[time] => 2018-08-01 14:15:00
)
[5] => Array
(
[user] => B
[time] => 2018-08-01 14:50:00
)
)
I was trying to find consecutive datetime count of user click within 10 min,Like user A click 4 times consecutively within 10 min and User B click only 1 time within 10 min
Array
(
[0] => Array
(
[user] => A
[count] => 4
)
[1] => Array
(
[user] => B
[count] => 1
)
)
You can use a function with your array and the $user you want to count as parameter, and then loop into the array and sum +1 for each interaction that matches your prerequisites.
Assuming your parent array is named $users, you can do the following:
function countConsecutive($users, $username)
{
$total = 0;
foreach($users as $user) {
if($user['user'] === $username && $user['time'] >= date('Y-m-d H:i:s', strtotime('-10 minutes'))) {
$total++;
}
}
return $total;
}
The return of the function will be the count of consecutive date-time from the user in the last 10 minutes.
This question already has answers here:
How to find the last day of the month from date?
(30 answers)
Closed 5 years ago.
We already created Weekly array
Like
[1] => Array
(
[start] => 2017-01-01
[end] => 2017-01-07
)
But we need monthly array start date and end date of month
Just Like this
Array
(
[1] => Array
(
[start] => 2017-01-01
[end] => 2017-02-01
)
[2] => Array
(
[start] => 2017-02-01
[end] => 2017-03-01
)
[3] => Array
(
[start] => 2017-03-01
[end] => 2017-04-01
)
[4] => Array
(
[start] => 2017-04-01
[end] => 2017-05-01
)
[5] => Array
(
[start] => 2017-05-01
[end] => 2017-06-01
)
)
Here we are using DateTime and DateInterval for achieving expected output.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$startDate="2017-01-01";
$endDate="2017-06-01";
$dates=array();
while($startDate!=$endDate)
{
$monthEndDate=new DateTime($startDate);
$monthEndDate->add(new DateInterval("P1M"));//adding one month each time we iterate
$dates[]=array("start" => $startDate,
"end" => $monthEndDate->format("Y-m-d"));
$startDate=$monthEndDate->format("Y-m-d");//changing start date
}
print_r($dates);
I have time with meridiem(AM/PM).
For example,
I have time
Array
(
[0] => 11:00 AM
[1] => 6:00 AM
)
So after sorting:
Array
(
[0] => 6:00 AM
[1] => 11:00 AM
)
And if I have:
Array
(
[0] => 11:00 AM
[1] => 6:00 PM
)
So sorting will be:
Array
(
[0] => 11:00 AM
[1] => 6:00 PM
)
I have tried with
sort($arr,SORT_LOCALE_STRING);
and
sort($arr,SORT_NUMERIC);
But not worked.
Any solution for this? Thanks.
Try this code.
usort($array, function($a, $b) {
return (strtotime($a) > strtotime($b));
});