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

Sorting an array

170 100+
If I have

$array1=array(3,7,12,56,89);
$array2=array(12,89,3,7,56);

Is it possible to sort $array1 so that it's in the same order as $array2? Ie. so $array1=array(12,89,3,7,56);
Apr 10 '09 #1
4 1144
Markus
6,050 Expert 4TB
@beary
Could you explain why? It seems to me you want 2 arrays with exactly the same content.. which seems slightly wasteful.
Apr 10 '09 #2
beary
170 100+
@Markus
Sure Markus,

array1 is an array of files
I have then had to do some mapping to get the correct order to display each of the files.
After that mapping, it turns out the correct order is the order of array2.
I can't just "refill" array1 with the actual files, so I need to sort array1 in the order of array2 before I display them. (There is no "natural" order to array2's files; it's not like they're alphabetically ordered or anything - that would make it easy)

Does this make sense?

I'm sure php can somehow do it. I have spent a number of hours on this, but have not had any success yet, and I decided it was time to get some more knowledgable people to help me :)
Apr 10 '09 #3
ghostdog74
511 Expert 256MB
@beary
you can just assign array1 the values of array2
Expand|Select|Wrap|Line Numbers
  1. $array1=array(3,7,12,56,89);
  2. print_r($array1);
  3. $array2=array(12,89,3,7,56);
  4. print_r($array2);
  5. $array1=$array2;
  6. print_r($array1);
  7.  
Apr 11 '09 #4
beary
170 100+
@ghostdog74
Thanks ghostdog,

I wish it were that easy... I already tried that. It breaks the whole thing. I think it's because the numbers are actually files. So I have this array of files that I need to sort, and so just making array1 (the files) = array2( the numbers representing the files) replaces files with numbers and it breaks. I really do need to be able to sort the original array1.
Apr 12 '09 #5

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

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
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...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.