473,413 Members | 1,679 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,413 software developers and data experts.

Sort array for key

Hi, I have the next problem. I want to sort the array for the key,
using a function. I have been looking for a function to operate with
arrays, but I haven't found anything.

Ej:

Array
(
[43] =001480-----3-----04/11/2003
[50] =001000-----3-----04/11/2005
)
I want to convert this array to:

Array
(
[1] =001480-----3-----04/11/2003
[2] =001480-----3-----04/11/2003
)

Thaks.
Feb 1 '08 #1
4 1646
On 1 Feb, 12:40, Kovu <kovut...@gmail.comwrote:
Hi, I have the next problem. I want to sort the array for the key,
using a function. I have been looking for a function to operate with
arrays, but I haven't found anything.

Ej:

Array
(
[43] =001480-----3-----04/11/2003
[50] =001000-----3-----04/11/2005
)

I want to convert this array to:

Array
(
[1] =001480-----3-----04/11/2003
[2] =001480-----3-----04/11/2003
)

Thaks.
Wow you must have looked really hard!
Did you even put
php array sort key
into Google?
Feb 1 '08 #2
Kovu wrote:
Hi, I have the next problem. I want to sort the array for the key,
using a function. I have been looking for a function to operate with
arrays, but I haven't found anything.

Ej:

Array
(
[43] =001480-----3-----04/11/2003
[50] =001000-----3-----04/11/2005
)
I want to convert this array to:

Array
(
[1] =001480-----3-----04/11/2003
[2] =001480-----3-----04/11/2003
)

IMO this is not even sorting, but "flushing" some array.
How hard can this be?

function arrayFlush($mixArray){
foreach($mixArray as $obj) $temp[] = $obj;
return $temp;
}

Or am I missing something?

--
Freundliche Grüße,
Franz Marksteiner

Feb 1 '08 #3
On Fri, 01 Feb 2008 18:10:53 +0100, Álvaro G. <Vicario"
<we*******************@demogracia.com>wrote:
*** Kovu escribió/wrote (Fri, 1 Feb 2008 04:40:45 -0800 (PST)):
>Hi, I have the next problem. I want to sort the array for the key,
>using a function. I have been looking for a function to operate with
arrays, but I haven't found anything.

Ej:

Array
(
[43] =001480-----3-----04/11/2003
[50] =001000-----3-----04/11/2005
)
I want to convert this array to:

Array
(
[1] =001480-----3-----04/11/2003
[2] =001480-----3-----04/11/2003
)
According to your example you want to sort an array discarding keys.
Not really, we have 2 different values here, I guess it's just a
preview/part of an array. Array index 50 would come before index 43 BTW,
001000 before 001480 offcourse trumps a later date. Or it's a typo. In
either case, difficult to interpret with 100% certainty. I'd advise the up
to look at the different functions able to sort arrays in
<http://nl2.php.net/manual/en/ref.array.php>

Offcourse, this dubious example could mean the OP wants to sort by the
ending date rather then the string value. Which would be something like:

function __mysort($a,$b){
$a_cmp = preg_match('%\d+/d+/\d+^%',$a,$a_match) ?
strtotime($a_match[0]):0;
$b_cmp = preg_match('%\d+/d+/\d+^%',$b,$b_match) ?
strtotime($b_match[0]):0;
if($a_cmp == $b_cmp) return 0;
return ($a_cmp < $b_cmp) ? -1 : 1;
}
usort($array,'__mysort');
--
Rik Wasmus
Feb 1 '08 #4
Greetings, Kovu.
In reply to Your message dated Friday, February 1, 2008, 15:40:45,
Hi, I have the next problem. I want to sort the array for the key,
using a function. I have been looking for a function to operate with
arrays, but I haven't found anything.
Ej:
Array
(
[43] =001480-----3-----04/11/2003
[50] =001000-----3-----04/11/2005
)
I want to convert this array to:
Array
(
[1] =001480-----3-----04/11/2003
[2] =001480-----3-----04/11/2003
)
Thaks.
If difference in the array values is a mistake and You want to just reindex
array values - try

$new = array_values($old);
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 7 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
4
by: Brett | last post by:
I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array. ie. @l1={3,1,2};...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
1
by: Marcus Kwok | last post by:
I wrote a little test program to demonstrate what may be a misunderstanding on my part in the behavior of ArrayList::Sort(). I defined a class (Test) that has two data members: a System::String*...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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...

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.