473,816 Members | 2,906 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 7026
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
1615
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
13594
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
2394
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
4180
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
5753
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
4830
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
1759
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
4962
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
9738
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9613
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
10409
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10433
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
10149
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
6900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4361
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
3889
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3033
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.