This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
Array ( [0] => stdClass Object ( [Id] => 18
[AccNo] => 1
[Title] => Hardware
[Description] => Mobile.
[ManuDate] => 8th July 1942
[MusCat] => Album
[month] => 7
[date] => 8 )
[1] => stdClass Object ( [Id] => 20
[AccNo] => 2
[Title] => Food
[Description] => Apple.
[ManuDate] => 27th July 1942
[MusCat] => Album
[month] => 7
[date] => 27 )
[2] => stdClass Object ( [Id] => 24
[AccNo] => 3
[Title] => Hardware
[Description] => Computer.
[ManuDate] => 2nd July 1942
[MusCat] => Album
[month] => 7
[date] => 2 )
[3] => stdClass Object ( [Id] => 56
[AccNo] => 4
[Title] => Hardware
[Description] => Printer
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 )
[4] => stdClass Object ( [Id] => 105
[AccNo] => 5
[Title] => Object
[Description] => Chair.
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 ) )
This is my array input Like
Id Date Title Description
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
2 2nd July 1942 Hardware Computer
3 1942 Hardware Printer
4 1942 Object Chair
I want output like
Id Date Title Description
************************************************************
3 1942 Hardware Printer
4 1942 Object Chair
2 2nd July 1942 Hardware Computer
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
How to sort multikey in php?
I am beginner in Php. I am using following code in php but out put will not correctly. If any one of datewise or monthwise sort, Output will come correctly. otherwise both (datewise or monthwise), Output will not come correctly. Plz help any solution.
usort($value['year'], function ($a, $b) {
if ($a->date == $b->date) return 0;
return $a->date < $b->date ? -1 : 1;
});
usort($value['year'], function ($a, $b) {
if ($a->month == $b->month) return 0;
return $a->month < $b->month ? -1 : 1;
});
The following code does the job. strtotime parses text date into a Unix timestamp.
usort($array, function($v1, $v2) {
$d1 = strlen($v1->ManuDate) === 4 ? '01-01-' . $v1->ManuDate : $v1->ManuDate;
$d2 = strlen($v2->ManuDate) === 4 ? '01-01-' . $v2->ManuDate : $v2->ManuDate;
return strtotime($d1) - strtotime($d2);
});
Related
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
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
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);
Basically I'm trying to sort a complex array of Objects within an array:
Array
(
[190515] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 190515
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350853200
[end_date_end_time] => 1350853200
[is_ongoing] => 0
[title] => Wentz Concert Hall and Fine Arts Center
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 190515
[venue_title] => Wentz Concert Hall and Fine Arts Center
[datepart] => 20121021
[occurrences] => 1
[times] => Sun 4:00pm
[end_times] => Sun 4:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 3
[occurrence_date] => 1350853200
)
)
[times_list] => Array
(
[0] => Oct 21 sun 4:00pm
)
)
[31403] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 31403
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350176400
[end_date_end_time] => 1350176400
[is_ongoing] => 0
[title] => KAM Isaiah Israel
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 31403
[venue_title] => KAM Isaiah Israel
[datepart] => 20121014
[occurrences] => 1
[times] => Sat 8:00pm
[end_times] => Sat 8:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 2
[occurrence_date] => 1350176400
)
)
[times_list] => Array
(
[0] => Oct 13 sat 8:00pm
)
)
[33861] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 33861
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350781200
[end_date_end_time] => 1350781200
[is_ongoing] => 0
[title] => Music Institute of Chicago, Nichols Concert Hall
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 33861
[venue_title] => Music Institute of Chicago, Nichols Concert Hall
[datepart] => 20121021
[occurrences] => 1
[times] => Sat 8:00pm
[end_times] => Sat 8:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 3
[occurrence_date] => 1350781200
)
)
[times_list] => Array
(
[0] => Oct 20 sat 8:00pm
)
)
)
I need to sort by occurrence_date and ensure that the data in the top Object (e.g.190515) doesn't become corrupted with other objects and to include the possibility that there could be a 2nd occurrence_date for the "times" [array] of Objects.
I've seen similar sort arrays of objects by field here, but not with the depth of the array/object. I tried using usort but I don't think my syntax to the value is correct.
Basically what I tried.
function cmp($x, $y) {
if ($x->occurrence_date > $y->occurrence_date) {
return 1; }
else {
return -1; }
}
usort($node->timeout_events_schedule->venues, 'cmp');
Thanks in advance
How about:
function compare($x,$y)
{
if($x->times[0]->occurrence_date == $y->times[0]->occurrence_date)
return 0;
elseif($x->times[0]->occurrence_date < $y->times[0]->occurrence_date)
return -1;
else
return 1;
}
uasort($your_array,'compare');
uasort() preserve your keys, unlike usort()
http://www.php.net/manual/en/function.uasort.php
Few tips to help you solve your problem:
you will need uasort function, to preserve associative keys
You can not access occurrence_date key directly from $x o r $y, you will need $x->times[0]->occurence_date
Before doing comparison, iterate through $x->times just to check if there are more entries, pick one that suits your needs.
since occurence_date is a number you don't have to use string comparison function
Good luck :)
I have this billing table from wherein I get records as per the report requirement.
The array I get is like this:
Array(
[0] => stdClass Object
(
[bid] => 3
[uid] => 2
[total_inc] => 100
[total_exp] => 55
[mon] => 1
[year] => 2012
[mstdatereg] => 2012-03-14
)
[1] => stdClass Object
(
[bid] => 2
[uid] => 3
[total_inc] => 85
[total_exp] => 45
[mon] => 1
[year] => 2012
[mstdatereg] => 2012-03-14
)
[2] => stdClass Object
(
[bid] => 1
[uid] => 8
[total_inc] => 130
[total_exp] => 75
[mon] => 1
[year] => 2012
[mstdatereg] => 2012-03-14
)
[3] => stdClass Object
(
[bid] => 5
[uid] => 25
[total_inc] => 130
[total_exp] => 65
[mon] => 2
[year] => 2012
[mstdatereg] => 2012-03-14
)
[4] => stdClass Object
(
[bid] => 4
[uid] => 27
[total_inc] => 75
[total_exp] => 50
[mon] => 2
[year] => 2012
[mstdatereg] => 2012-03-14
)
[5] => stdClass Object
(
[bid] => 10
[uid] => 3
[total_inc] => 180
[total_exp] => 100
[mon] => 3
[year] => 2012
[mstdatereg] => 2012-04-05
)
[6] => stdClass Object
(
[bid] => 6
[uid] => 12
[total_inc] => 60
[total_exp] => 35
[mon] => 3
[year] => 2012
[mstdatereg] => 2012-03-14
)
[7] => stdClass Object
(
[bid] => 7
[uid] => 22
[total_inc] => 160
[total_exp] => 90
[mon] => 3
[year] => 2012
[mstdatereg] => 2012-03-14
)
[8] => stdClass Object
(
[bid] => 9
[uid] => 3
[total_inc] => 115
[total_exp] => 70
[mon] => 4
[year] => 2012
[mstdatereg] => 2012-03-16
)
)
What I have done through looping is the result which is like this:
January, 2012
==========
Income Expense
2 100 55
3 85 45
8 130 75
---------------------------------
Total 315 175
February, 2012
===========
Income Expense
25 130 65
27 75 50
---------------------------------
Total 205 115
March, 2012
Income Expense
3 180 100
12 60 35
22 160 90
---------------------------------
Total 400 225
April, 2012
Income Expense
3 115 70
---------------------------------
Total 115 70
Net Total Income: 1035
Net Total Expense: 585code here
However what I want is
Sr.No Member 1, 2012 2, 2012 3, 2012 4, 2012 Total
Inc|Exp Inc|Exp Inc|Exp Inc|Exp Inc|Exp
=======================================================================
1 2 100|55 100|55
2 3 85|45 180|100 115|70 380|215
3 8 130|75 130|75
4 25 130|65 130|65
5 27 75|50 75|50
6 12 60|35 60|35
7 22 160|90 160|90
=======================================================================
Total 315|175 205|115 400|225 115|70 1035|585
Prior to posting, I searched for such problem and this is what I found: Building a "crosstab" or "pivot" table from an array in php
I tried to make it work according to my requirement but cud not do so.
Been trying for many days now. Any help will be appreciated.
I think you'll have better luck using SQL to do most of this number crunching for you.
Relatively simple process, then:
Determine the months (columns) you want to represent in the table.
Use GROUP BY to break your data into rows by Sr. No (or whatever you want to use)
Use WHERE clause to cut out data that's not in the date range you've chosen (i.e. if Report is only for Feb & March, don't include Jan)
Totals column is sum of Income or Expense for given row (remember, rows are grouped using criteria you chose earlier)
Individual month column is sum of IF(Date is in this column's month, Amount, 0)
Totals row is calculated in your PHP
You can generate the month column code programmatically.
Below's an example of some code to generate the query for a given set of month-year combos (untested). It's trivial to get it into a table from here (you just need to calculate the totals row in PHP).
$qstr = 'SELECT bid, uid, ';
$reportMonths = array(1 => 2012, 2 => 2012, 3 => 2012, 4 => 2012);
foreach ($reportMonths as $mon => $year) {
$qstr .= "SUM(IF(mon=$mon AND year=$year),total_inc,0)) as $mon-inc, SUM(IF(mon=$mon AND year=$year,total_exp,0)) as $mon-exp, ";
}
$qstr .= 'SUM(total_inc) as total-inc, SUM(total_exp) as total-exp from bill_mst WHERE ';
foreach ($reportMonths as $mon => $year) {
$qstr .= '(mon=$mon AND year=$year) OR ';
}
// chop off last or
$qstr = substr($qstr, 0, -3);
$qstr .= 'GROUP BY bid, uid ASC';
$res = mysql_query($qstr);