I've made this code. To show the data based on same date (day)
$group = array();
$stmt = $conn->prepare("
SELECT *,date(date_added) as dateadded
FROM rr
ORDER BY dateadded DESC
");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($res = $stmt->fetch()){
$rtype = array();
$rtype['type'] = "date";
$rtype['date'] = date_format (new DateTime($res['dateadded']), 'M jS, Y');
$rtype[] = $res;
$result[] = $rtype;
}
What I got as result from query sql
array
(
[0] => Array
(
[type] => date
[date] => Dec 18th, 2019
[0] => Array
(
[id] => 1
[user_id] => 1
[status] => 1
[date_added] => 2019-12-18 13:44:30
[dateadded] => 2019-12-18
)
)
[1] => Array
(
[type] => date
[date] => Dec 20th, 2019
[0] => Array
(
[id] => 3
[user_id] => 2
[status] => 2
[date_added] => 2019-12-20 14:53:04
[dateadded] => 2019-12-20
)
)
[2] => Array
(
[type] => date
[date] => Dec 29th, 2019
[0] => Array
(
[id] => 5
[user_id] => 3
[status] => 0
[date_added] => 2019-12-29 00:39:21
[dateadded] => 2019-12-29
)
)
)
There is some data that is not show as result in the same date.
What I want to show is like this.
Array
(
[0] => Array
(
[type] => date
[date] => Dec 18th, 2019
)
[1] => Array
(
[id] => 2
[user_id] => 1
[status] => 1
[date_added] => 2019-12-18 13:44:30
)
[2] => Array
(
[type] => date
[date] => Dec 20th, 2019
)
[3] => Array
(
[id] => 1
[user_id] => 1
[status] => 0
[date_added] => 2019-12-20 14:53:04
)
[4] => Array
(
[id] => 3
[user_id] => 1
[status] => 0
[date_added] => 2019-12-20 14:53:04
)
)
The result is become an array like that with group by date (day).
How to get this result?
Thanks for helping me.
You need to set $group_date to the value of $res['dateadded'] so you can tell when the date changes. There's no need to use substr(), since dateadded just contains the date without a time.
You should push $res onto $result, not $rtype.
$group_date = "";
$group = array();
$stmt = $conn->prepare("
SELECT *,date(date_added) as dateadded
FROM rr
ORDER BY dateadded
");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($res = $stmt->fetch()){
$rtype = array();
if ($group_date !== $res["dateadded"]) {
$rtype['type'] = "date";
$rtype['date'] = date_format (new DateTime($res['dateadded']), 'M jS, Y');
$result[] = $rtype;
$group_date = $res["dateadded"];
}
$result[] = $res;
}
Related
I want to group results by month, to be precise Sum of total prices for each month, but i don't understand how i can divide months, then Sum price for each month.
This is required for morris chart like this
$data2[] = array(
'y' => $month,
'a' => price,
'b' => ''
);
I was using before query to get this results and was a easy way to do it, but now with code is different story.
Any help with explanation of how each segment of code is work will be so nice, so i can avoid problems like this one in future.
I need something like this :
$month = [May, June];
$price = [Sum of all prices for May, Sum of all prices for June];
This is a code :
$monthsArray = array();
// get all dates in array
foreach ($array as $key) {
// convert dates to short format, month-year -> Jun-18
$monthsArray[] = date('M-y', strtotime($key['time']));
}
$months = array();
foreach ($monthsArray as $date) {
$mon = substr($date, 0, 6);
if (!in_array($mon, $months)) array_push($months, $mon);
}
foreach ($months as $m) {
// final array for chart
$data2[] = array('y'=>$m, 'a'=>'', 'b'=>'');
}
$morris = json_encode($data2);
This is array i have
Array
(
[0] => Array
(
[price] => -1835.25
[time] => 2018-05-29 16:53:38
)
[1] => Array
(
[price] => -1743.52
[time] => 2018-05-29 16:53:39
)
[2] => Array
(
[price] => -4445.55
[time] => 2018-05-31 13:21:04
)
[3] => Array
(
[price] => -34647.04
[time] => 2018-05-31 18:29:43
)
[4] => Array
(
[price] => 16888.41
[time] => 2018-06-01 15:05:24
)
[5] => Array
(
[price] => 14369.05
[time] => 2018-06-07 14:44:21
)
[6] => Array
(
[price] => -49579.69
[time] => 2018-06-11 09:14:42
)
[7] => Array
(
[price] => -33300.94
[time] => 2018-06-08 23:50:29
)
[8] => Array
(
[price] => 4413.21
[time] => 2018-06-12 07:15:52
)
[9] => Array
(
[price] => 2724.69
[time] => 2018-06-12 07:15:46
)
[10] => Array
(
[price] => 10224.03
[time] => 2018-06-08 14:00:13
)
[11] => Array
(
[price] => -797.92
[time] => 2018-06-08 13:54:08
)
[12] => Array
(
[price] => -25157.34
[time] => 2018-06-11 11:31:31
)
[13] => Array
(
[price] => 2701.6
[time] => 2018-06-11 14:32:08
)
[14] => Array
(
[price] => 2038.92
[time] => 2018-06-12 07:15:48
)
[15] => Array
(
[price] => -10541.58
[time] => 2018-06-15 10:35:58
)
)
EDIT:
I manage to group months as you can see in updated code and now i am getting only 2 results as i should be, now its just a issue with SUM of data for each month.
This is what i am getting in console
0: {y: "May-18", a: "", b: ""}
1: {y: "Jun-18", a: "", b: ""}
I have following 'challange';
I have array like this:
Array
(
[0] => stdClass Object
(
[id] => 94
[day] => Monday
[date] => 2018-07-09
[week_number] => 2
)
[1] => stdClass Object
(
[id] => 95
[day] => Tuesday
[date] => 2018-07-10
[week_number] => 2
)
[2] => stdClass Object
(
[id] => 83
[day] => Saturday
[date] => 2018-07-07
[week_number] => 1
)
[3] => stdClass Object
(
[id] => 82
[day] => Friday
[date] => 2018-07-06
[week_number] => 1
)
[4] => stdClass Object
(
[id] => 81
[day] => Thursday
[date] => 2018-07-05
[week_number] => 1
)
[5] => stdClass Object
(
[id] => 80
[day] => Wednesday
[date] => 2018-07-04
[week_number] => 1
)
)
I wanted to know how many times user have selected "week_number" 1,2 and so on, I don't want to allow user to select more than 3 events in a week.
I am using fullcalendar to display events.
How can I achieve that?
Thank you in advance
In PHP 7 you can extract the week_number properties and count the values:
$result = array_count_values(array_column($array, 'week_number'));
Will yield the week_number as the key and the count as the value:
array
(
[1] => 4
[2] => 2
)
Depending upon wheteher you want to check for multiples or just one, loop and check for > 3 or use in_array(3, $result).
You might use array_reduce to
$result = array_reduce($input, function($outpu, $item) {
if(!isset($output[$item->week_number])) {
$output[$item->week_number] = 0;
}
return $output[$item->week_number]++;
});
var_dump($result);
Where $input is You array of objects
If you only need to check if there are more than x events, and you don't need to count all events, better use something like this:
function hasTooManySelections($items, $maxSelections = 3)
{
$counts = [];
foreach ($items as $item) {
$counts[$item->week_number] = isset($counts[$item->week_number]) ? $counts[$item->week_number] + 1 : 1;
if ($counts[$item->week_number] > $maxSelections) {
return true;
}
}
return false;
}
var_dump(hasTooManySelections($items));
Demo: https://3v4l.org/XbiH0
You can sort your array in respect of group by week number and then count length of all perticular group
I want to split items which have same date and different date.
foreach ($cart->getAllItems() as $item)
{
$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime();
$pickupDateTime = date('Y-m-d G:i:s', strtotime($pickupDateTime)) //2018-03-09 6:03:00 or 2018-03-09 21:10:00
// Split items here with same Date //
if(Dates are Same Condition){
$items_normal[]= $item->getProductId();
}else
{
//If Dates Are diffrent
$ites_special[]= $item->getProductId();
}
}
Need to compare if dates are same in first if condition and in else part the items which have different dates.
Look at the following arrays, in this I have same date and time, I need to club those which have same date and time. In below example, the 3rd one must be come under first Array as it has different time i.e. 6:05 instead of 6:03
Array
(
[1] => Array
(
[product_id] => 742
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
[2] => Array
(
[product_id] => 743
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
)
Array
(
[3] => Array
(
[product_id] => 744
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0.12
[splinst] => null
)
[4] => Array
(
[product_id] => 757
[qty] => 1
[date] => 03/09/2018
[time] => 06:05 AM
[tax] => 0.25
[splinst] => null
)
)
You should use the DateTime object:
$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime();
$pickupDateTime = new DateTime($pickupDateTime);
You can compare two DateTime objects which each other:
if ($dateTimeObject1 == $dateTimeObject2) {
// both have same date and time
}
Be aware that you should not use strict comparison (===) here as it would check if it is the same object and not if the DateTime is the same.
If you need to compare only the date and ignore the time you can call setTime on both objects to reset the time to 0:
$dateTimeObject1->setTime(0, 0, 0);
$dateTimeObject2->setTime(0, 0, 0);
if ($dateTimeObject1 == $dateTimeObject2) {
// both have the same date
}
You could store the date in keys into an array to group items :
foreach ($cart->getAllItems() as $item)
{
$pickupDateTime = $item->getCartPickupDate().' '.$itemgetCartPickupTime();
$pickupDateTime = date('Y-m-d G:i:s', strtotime($pickupDateTime)) //2018-03-09 6:03:00 or 2018-03-09 21:10:00
$items_special[$pickupDateTime][] = $item->getProductId();
}
foreach ($items_special as $date => $items) {
if (count($items) > 1) {
$items_normal = $items ;
unset($items_special[$date]);
break;
}
}
$items_special = array_values($items_special);
So, $items_special will look like :
Array
(
[0] => Array
(
[product_id] => 757
[qty] => 1
[date] => 03/09/2018
[time] => 06:05 AM
[tax] => 0.25
[splinst] => null
)
)
And $items_normal will look like :
Array
(
[0] => Array
(
[product_id] => 742
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
[1] => Array
(
[product_id] => 743
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0
[splinst] => null
)
[2] => Array
(
[product_id] => 744
[qty] => 1
[date] => 03/09/2018
[time] => 06:03 AM
[tax] => 0.12
[splinst] => null
)
)
$merged = array();
while($row = mysqli_fetch_assoc($get_data_res)){
$merged[] = $row;
}
echo '<pre>'; print_r($merged); echo '</pre>';
After Printing Array I am getting this output
Array
(
[0] => Array
(
[lees_than_60] => 50
[one_four] => 48
[four_min] => 0
[id] => 24
[first_name] => Yogini
[log_in_time] => 0000-00-00 00:00:00
[log_out_time] => 0000-00-00 00:00:00
[calldate] => 2014-05-22
[clid] => 2433
)
[1] => Array
(
[lees_than_60] => 14
[one_four] => 6
[four_min] => 2
[id] => 28
[first_name] => Vijay
[log_in_time] => 2014-05-28 16:57:51
[log_out_time] => 2014-05-28 16:58:12
[calldate] => 2014-05-22
[clid] => 2436
)
[2] => Array
(
[lees_than_60] => 64
[one_four] => 60
[four_min] => 0
[id] => 23
[first_name] => Pratibha
[log_in_time] => 2014-05-28 15:55:18
[log_out_time] => 2014-05-28 15:55:44
[calldate] => 2014-05-22
[clid] => 2431
)
)
My question is I want to create below array like I want to create new array against same calldate 2014-05-22 ie only one calldate and remaining all data like:
[calldate] => 2014-05-22
show whole data of aove call date should not repeat by merging
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 2 months ago.
I need some help about sorting a multiple array.
This is what I got:
Array (
[ALU0000001] =>
Array ( [0] => Array ( [period] => 2012 [codCurse] => S12-2030 [idPersona] => ALU0000001 [date] => 2012-04-02 [amount] => 238.00 [active] => X )
[1] => Array ( [period] => 2012 [codCurse] => S12-2030 [idPersona] => ALU0000001 [date] => 2012-05-02 [amount] => 238.00 [active] => X )
[2] => Array ( [period] => 2012 [codCurso] => S12-2030 [idPersona] => ALU0000001 [date] => 2012-06-02 [amount] => 238.00 [active] => X )
[3] => Array ( [period] => 2013 [codCurso] => S12-2030 [idPersona] => ALU0000001 [date] => 2013-01-02 [amount] => 238.00 [active] => X )
[ALU0000005] =>
Array ( [0] => Array ( [period] => 2013 [codCurse] => S13-2010 [idPersona] => ALU0000005 [date] => 2013-03-01 [amount] => 225.00 [active] => X )
[1] => Array ( [period] => 2013 [codCurse] => S13-2010 [idPersona] => ALU0000005 [date] => 2013-03-02 [amount] => 333.00 [active] => X )
[2] => Array ( [period] => 2013 [codCurse] => S13-2010 [idPersona] => ALU0000005 [date] => 2013-04-02 [amount] => 333.00 [active] => X )
I need to sort multiarray by period date to get something like this
Y M D
2012 2012-04-02 ALU00000001 .....
2012 2012-05-02 ALU00000005 .....
2012 2012-06-01 ALU00000001 .....
2013 2013-01-01 ALU00000001 .....
2013 2013-06-01 ALU00000001 .....
2013 2013-12-24 ALU00000005 .....
Thanks
As danp says you will need a custom sort function using PHP usort.
You execute this in Codeigniter using the syntax;
usort($data_array, array('controller', 'sort_function'));
function sort_function($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
Considering the main array keys don't do much (because they're already contained within the values by the date key) you can safely ignore them. So first you want to collect all the values into a single array to be able to sort later:
$allItems = array();
foreach ($outputArr as $arr) { // $outputArr should be the name of your array
$allItems = array_merge($allItems,array_values($arr));
}
Then you need to sort the array values by the date keys:
function sortByDate($a,$b) {
$d1 = strtotime($a['date']);
$d2 = strtotime($b['date']);
return $d1 == $d2 ? 0 : ($d1 > $d2 ? 1 : -1);
}
usort($allItems,'sortByDate');
// and there you go.
print_r($allItems);