troubleshooting Question

Rearrange Arrays in PHP

Avatar of OmniUnlimited
OmniUnlimitedFlag for United States of America asked on
PHP
23 Comments4 Solutions538 ViewsLast Modified:
Happy New Year Experts!

I hope this question finds you all well and ready for a fantastic new year.

I'd like to start out simple this year (simple at least to you PHP geniuses out there.)

I have a multidimentional associative array, let's call it $A.  This array can be accessed by using two keys such as $A[$key1][$key2].

Right now, the order of the elements goes something like this:

$A['first']['e1']
$A['first']['e2']
$A['first']['e3']
$A['first']['e4']
$A['first']['e5']
$A['first']['e6']
$A['second']['e1']
$A['second']['e2']
$A['second']['e3']
$A['second']['e4']
$A['second']['e5']
$A['second']['e6']
$A['third']['e1']
$A['third']['e2']
$A['third']['e3']
$A['third']['e4']
$A['third']['e5']
$A['third']['e6']
...etc.

I would like to create a new array grouping all the "e1" elements together, then the "e2" elements and so on, so that the new array would look something like the following:

$NewArray['e1'] = array($A['first']['e1'], $A['second']['e1'], $A['third']['e1'], etc...);
$NewArray['e2'] = array($A['first']['e2'], $A['second']['e2'], $A['third']['e2'], etc...);
$NewArray['e3'] = array($A['first']['e3'], $A['second']['e3'], $A['third']['e3'], etc...);
etc...

What is the simplest, most elegant and efficient solution to this?

Thanks!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 4 Answers and 23 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros