PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
xkevin
Forum Newbie
Posts: 14 Joined: Thu Jun 09, 2016 9:01 pm
Post
by xkevin » Mon Jun 13, 2016 8:50 pm
My current code:
Code: Select all
$masterData = array();
$data = file("datas/payments.csv");
foreach ($data as $deposit){
$depositarray = explode(",", $deposit);
$depositlist = $depositarray;
$key = md5($depositlist[9] . number_format($depositlist[10],2)); //date + amount
$masterData[$key]['payment'] = array(
'name' => $depositlist[0],
'email' => $depositlist[1],
'depositdate' => $depositlist[9],
'depositamount' => number_format($depositlist[10],2)
);
}
usort($masterData[$key]['payment']['depositdate']);// (added)What ive tried but not working
I display it like this:
Code: Select all
foreach($masterData as $key=>$data) {
<td><?=($data['payment']['name'])?></td>
<td><?=($data['payment']['email'])?></td>
<td><?=($data['payment']['depositdate'])?></td>
<td><?=($data['payment']['depositamount'])?></td>
}
I would like to sort it ascending by date 'depositdate'. So that it is arranged properly when I display the list. Thank you for help.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Mon Jun 13, 2016 9:47 pm
usort() takes two arguments. Look at the documentation to see what you need to do about that.
xkevin
Forum Newbie
Posts: 14 Joined: Thu Jun 09, 2016 9:01 pm
Post
by xkevin » Mon Jun 13, 2016 10:13 pm
requinix wrote: usort() takes two arguments. Look at the documentation to see what you need to do about that.
I added function like this
Code: Select all
function cmp($date, $date)
{
if ($date == $date) {
return 0;
}
return ($date < $date) ? -1 : 1;
}
at the end of loop, I added this:
Code: Select all
$date = $masterData[$key]['payment']['depositdate'];
usort($date,"cmp");
-Any help about my code?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Jun 14, 2016 12:46 am
Ah, I missed that the first time.
usort() is for sorting arrays . You need to give it the array that you want to sort. Not a single value you got from somewhere inside it.
Your foreach loop is over $masterData. So that's what you need to sort.
xkevin
Forum Newbie
Posts: 14 Joined: Thu Jun 09, 2016 9:01 pm
Post
by xkevin » Tue Jun 14, 2016 2:44 am
uhm..sorry, i didn't get it. Can you help me with my code? It my first time dealing with sorting