473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

sorting multidimensianal array based on the date field

pradeepjain
563 512MB
Hello Guys,
I am using a multidimensional array in which there is a date field also..i want to sort the multidimensional array based on this date filed how to do this.



Thanks,
Pradeep
Jun 6 '08 #1
7 6991
dlite922
1,584 Expert 1GB
In what level is the date field? In the top array? or nested array?

that would help.

see array_sort() in PHP docs too!

Cheers,


Dan
Jun 6 '08 #2
pradeepjain
563 512MB
my array is in this format
$tmp3 = array(
'Count' => $i+1,
'Description' => $sub_row['Description'],
'Posted' => $sub_row['2'],
'Expires' => $sub_row['3'],
'Type' => $sub_row['Type'],
'ID' => $sub_row['ID'],
'Doc' => $sub_row['Doc'],
'Doc1' => $sub_row['Doc1'],
'Doc2' => $sub_row['Doc2'],
'Name' => $sub_row['Name'],
'Name1' => $sub_row['Name1'],
'Name2' => $sub_row['Name2']


);
where Posted is in Date format ...i need to sort this array based on the Posted value ...how to do this


Cheers,
Pradeep
Jun 7 '08 #3
Markus
6,050 Expert 4TB
There's no such array_sort() function, unfortunately. asort().
Jun 7 '08 #4
dlite922
1,584 Expert 1GB
my array is in this format
$tmp3 = array(
'Count' => $i+1,
'Description' => $sub_row['Description'],
'Posted' => $sub_row['2'],
'Expires' => $sub_row['3'],
'Type' => $sub_row['Type'],
'ID' => $sub_row['ID'],
'Doc' => $sub_row['Doc'],
'Doc1' => $sub_row['Doc1'],
'Doc2' => $sub_row['Doc2'],
'Name' => $sub_row['Name'],
'Name1' => $sub_row['Name1'],
'Name2' => $sub_row['Name2']


);
where Posted is in Date format ...i need to sort this array based on the Posted value ...how to do this


Cheers,
Pradeep
okay fine array_sort() doesn't exist, but i know this exists!

http://us2.php.net/manual/en/functio...-multisort.php

That should be what your looking for. Let us know if you don't know how to use it, or can't get it to work.

Cheers,


Dan
Jun 7 '08 #5
dlite922
1,584 Expert 1GB
okay fine array_sort() doesn't exist, but i know this exists!

http://us2.php.net/manual/en/functio...-multisort.php

That should be what your looking for. Let us know if you don't know how to use it, or can't get it to work.

Cheers,


Dan
Okay i'll be nice, here's an example:

[PHP] <?php
// sample array:
$data = array(
array('date' => '2008-06-07','name'=>'name4'),
array('date' => '2008-06-11','name'=>'name2'),
array('date' => '2009-05-30','name'=>'name5'),
array('date' => '2008-02-02','name'=>'name1'),
array('date' => '2007-01-11','name'=>'name3'),
);

// Obtain a list of columns
foreach ($data as $key => $row) {
$date[$key] = $row['date'];
}
// Sort the data by $date
// Add $data as the last parameter,
array_multisort($date, $data);
print_r($data);
?>

[/PHP]

you can even do a secondary sort by the name. For example if you have two dates the same, in what order do you want 'name' to be in.

Cheers,


Dan
Jun 7 '08 #6
pradeepjain
563 512MB
[PHP]function record_sort($records, $field, $reverse=false)
{
$hash = array();

foreach($records as $record)
{
$hash[$record[$field]] = $record;
}

($reverse)? krsort($hash) : ksort($hash);

$records = array();

foreach($hash as $record)
{
$records []= $record;
}

return $records;
}
$coll = record_sort($coll, "name");
[/PHP]

i was using this to do sorting everytime and only for the date format its not working .....
can we modify it..

Thanks,
Pradeep
Jun 9 '08 #7
pradeepjain
563 512MB
srry guys the script tht i gave u is working fine,....i had only made a small mistake....thanks for all ur help


Thanks,
Pradeep
Jun 10 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: LRW | last post by:
Subject line confusing? The whole thing is confusing to me. OK, I think the root of my problem, aside from mental, is array based...but I'm not even sure what I want to do is even possible. So if...
3
by: Raghuram Banda | last post by:
Hi, I was strucked in a problem, basically I'm working on Sortable Table and the table is created using HTML Tags and one of the fields in the table contain Date in unix format (something like...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
pradeepjain
by: pradeepjain | last post by:
Hii, i need help in sorting multidimensianal array... i Read the multi sort function cld not use tht so i need help..This is the code. here the problem is tht i need to take out values...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.