473,800 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting multidimensiana l array based on the date field

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



Thanks,
Pradeep
Jun 6 '08 #1
7 7024
dlite922
1,584 Recognized Expert Top Contributor
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 Contributor
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 Recognized Expert Expert
There's no such array_sort() function, unfortunately. asort().
Jun 7 '08 #4
dlite922
1,584 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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'=>'na me4'),
array('date' => '2008-06-11','name'=>'na me2'),
array('date' => '2009-05-30','name'=>'na me5'),
array('date' => '2008-02-02','name'=>'na me1'),
array('date' => '2007-01-11','name'=>'na me3'),
);

// 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 Contributor
[PHP]function record_sort($re cords, $field, $reverse=false)
{
$hash = array();

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

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

$records = array();

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

return $records;
}
$coll = record_sort($co ll, "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 Contributor
srry guys the script tht i gave u is working fine,....i had only made a small mistake....than ks 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
1611
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 I can get far enough to know that one way or the other, then I can try to figure out how to do it. But here's the situation: Page 1 I have a form with many item rows pulled from a database query. Among other fields, each row contains a...
3
13592
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 Tue Sep 02 01:00:00 2003). I wrote the script using DOM. I collected innerHTML of a particular cell and checked whether it a string or not (using isNaN) and based on that I sorted the rows based on a column. Coming to this date field my sort...
12
2393
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 field... This grid displayes results based on users search.. public static int numberDiv; private void Page_Load(object sender, System.EventArgs e) {
22
4174
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) { return -1; } else
18
5752
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 not in chronological order. Is there a way to first sort the collection and put the dates in chronological order before creating the value list string? Or, how would I iterate over the collection pulling out the dates in chronological order? ...
7
4829
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 expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
1
1757
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 from 2 different tables.i am getting the output properly but only thing is tht i need to sort the o/p as per name here fid=6 is designation..uid is the id of the user ,fid=3 is Name of the person,fid=11 is for extension ,fid=1 is research...
5
4960
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.