Sort array & keep corresponding array in sorted sequence

I have this case:

I have two arrays with the same number of elements; array 1 contains part numbers, array 2 contains quantities of the corresponding part number.

I want to sort the part # (alphanumeric) array & keep the corresponding quantity array "in synch".

Is there a php function that will do this or do I have to program the sort "manually"?

Richard KortsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You might want to change your data design strategy.  Keep a single array.  Let the array contain objects and in these objects store the part numbers and the quantities as properties.  Then you can use usort() to sort on any of the object properties and the objects will still remain intact, with the correct properties associated together.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
To answer the question - no there is no PHP function that will do that directly.

If you are unable / constrained in following Ray's advice then what you need to do is to merge the two arrays into one and then sort that.

The process for doing this is dependent on what is in your arrays - if they are just scalars it is a fairly simple process of looping through one and creating a new array with the index equal to the value in the first array and the value the corresponding value in the second for instance (assumes non-associative arrays)
$merged = array();
foreach($parts as $k => $p) {
   $merged[$p] = $quantities[$k]
// sort here

Open in new window

If however, your arrays contain arrays or objects then a different mechanism would need to be used.

Can you give us a breakdown of exactly what is in each array?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.