php array sorting by date stdClass Object - php

I have an array of something like this:
Array ( [0] => stdClass Object ( [id] => 41 [title] => test2 [alias] => test2 [catid] => 8 [published] => 1 [introtext] =>
test2
[fulltext] => [video] => [gallery] => [extra_fields] => [] [extra_fields_search] => [created] => 2012-08-27 16:37:51 [created_by] => 62 [created_by_alias] => [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 [modified_by] => 0 [publish_up] => 2012-08-27 16:37:51 [publish_down] => 0000-00-00 00:00:00 [trash] => 0 [access] => 1 [ordering] => 15 [featured] => 0 [featured_ordering] => 0 [image_caption] => [image_credits] => [video_caption] => [video_credits] => [hits] => 0 [params] => JParameter Object ( [_raw:protected] =>
and etc in that array ( it has alot of things in it ).
Now it is displaying like this
Item | date
Item | date
Item | date
What I want to do is take that array and sort it by aggregate date
Somethink like this
Aggr date
Item | date
Item | date
Aggr date
Item | date
Item | date
Is this even possible given this array ?

Is this what you are looking for?
$newArray = array();
foreach ($myArrayOfStdClasses as $key => $obj) {
$newArray[date('Y-m-d', strtotime($obj->created]))[] = array('title' => $obj->tile, 'date' => $obj->created);
}

You can use usort to define your sorting function:
usort($items, function($item1, $item2){
if($item1->created == $item2->created)
return 0;
return $item1->created < $item2->created ? -1 : 1;
});
This will just sort them. Then on output as you loop through them you can do the aggregation based on day, hour, month however...

Related

Check if multiple datetime are same in php foreach

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
)
)

how to count common values ina array

Array (
[attendance_id] => 18
[attendance] => 1
[student_id] => 1
[date] => 2015-01-19
[in_time] => 00:00:00
[out_time] => 00:00:00
[fee_amount] => 15000 )
Array (
[attendance_id] => 19
[attendance] => 1
[student_id] => 2
[date] => 2015-01-19
[in_time] => 00:00:00
[out_time] => 00:00:00
[fee_amount] => 2000 )
Array (
[attendance_id] => 20
[attendance] => 0
[student_id] => 1
[date] => 2014-01-15
[in_time] => 00:00:00
[out_time] => 00:00:00
[fee_amount] => 0
)
I want to count the common values in these Arrays. Please guide how to do this.
As result I want to get this:
date [2014-01-15 ] => 2
attendance [1] => 2
This is actually for a small institute where single student details for a day may entered into the system as above. So I just want to make a report day end saying how many students has come to the class on specific day and how many didn't.
Try this..
<?php
$array = array(array('18','2015-01-19','15000'),array('2015-01-19','18','22'),array('20','11','22'));
$newcountarray = array();
foreach ($array as $key=>$value) {
foreach ($value as $newvalue) {
if ($foundKey = array_key_exists($newvalue,$newcountarray)) {
$newcountarray[$newvalue] += 1;
}
else{
$newcountarray[$newvalue] = 1;
}
}}
print_r($newcountarray);
?>
Output
Array ( [18] => 2 [2015-01-19] => 2 [15000] => 1 [22] => 2 [20] => 1 [11] => 1 )
Yes Deena Let's say
$array = array(array('18',<==row id no
'2015-01-19',<=date
'1',<=student id>
'2'<=present(1),absent(2))),
array(array('19',<==row id no
'2015-01-19',<=date
'2',<=student id>
'1'<=present(1),absent(2))),
its like for the day 2015-01-19 i want to get all 2 stuudent and among them 1 Student is present and 1 is absent

Read an array and merge new new agaist same key and value php

$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

Sort Multiarray in php [duplicate]

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);

PHP - Access MSSQL datetime column from the returned array

PHP - Access MSSQL datetime column from the returned array
Array
(
[0] => Array
(
[CallId] => 45
[CallLoginId] => 1
[CustomerId] => 140
[CallOptionId] => 2
[CallTypeId] => 2
[CallStatusId] => 1
[CallDateTime] => DateTime Object
(
[date] => 2012-06-28 00:00:00
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[ContactNo] => 45151551115
[ContactPerson] => Contact Person name
[ProductId] => 1
[ProdCompanyId] => 1
[ProdCategoryId] => 1
[ModelNo] => 451212151
[ProdUnderId] => 1
[Problem] => Simple Problem Details
[Remarks] => Remarks
[Accessories] => Accessories
[CallCaseId] =>
[CallCaseDate] =>
[ServiceCharge] => 0
[CourierName] =>
[DocketNo] =>
[CompanyId] => 126
[ASPId] => 130
[InsBy] => 134
[InsDate] => DateTime Object
(
[date] => 2012-06-23 17:04:51
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[UpdBy] => 11
[UpdDate] => DateTime Object
(
[date] => 2012-06-28 18:29:23
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[FName] => Kumar
[MName] => a
[LName] => Customer
[ProductName] => LenovoDesktop420
[CallOption] => InHouse
[CallType] => H.W.Installation
[ProdCompany] => Lenovo
[ProdCategory] => Desktop
[ProdUnder] => AMC
[CallStatus] => Open
[EntityId] => 134
[InsertBy] => Bhavin Rana
)
)
how can access date time value form this returned array ?
thanks in advance.
You have an outer array with numerically indexed one element [0], which is an associative array. CallDateTime as a key of that array is a DateTime object.
$array[0]['CallDateTime']->format('Y-m-d H:i:s') // 2012-06-28 00:00:00
$array[0]['CallDateTime']->getTimezone()->getName() // Asia/Kolkata
// Same with InsDate and UpdDate
$array[0]['InsDate']->format('Y-m-d H:i:s') // 2012-06-23 17:04:51
$array[0]['InsDate']->getTimezone()->getName() // Asia/Kolkata

Categories