By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

Array sort/merge

100+
P: 107
hey,

I have a listing of active directory groups in an array by distinguished name, like so:


Expand|Select|Wrap|Line Numbers
  1. myar[0] "CN=my sub group, CN=my group, OU=myou"
  2. myar[1] "CN=another subgroup, CN=my group, OU=myou"
  3. myar[3] "CN=diff group, OU=myou"
  4. myar[4] "CN=foo group, OU=ou2"
  5. myar[5] "CN=wowgroup, CN=foo group, OU=ou2"
  6.  

I want to convert it into a multi dimentional array with the objects as the arrays keys and in reverse order, so it gourps them like in the following format:

Expand|Select|Wrap|Line Numbers
  1.  
  2. arr['myou']['mygroup']['my sub group']
  3. ----------------------['another sub group']
  4. -----------['diff group']
  5. arr['ou2']-['foo group']['wowgroup']
  6.  
  7.  

Yea, sorry that looks well confusing... i basically want to group them into an array by category starting at the end. So the first key of the array would be "myou", and that would contain an array of any other groups in that category etc.



Andy
Sep 24 '08 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Andy.

The way I'd probably approach that is to go through each element in your array, and do a str_replace(', ', '&', ... ) on it. Then run it through parse_str() and go from there (http://php.net/parse_str).

Something like this:
Expand|Select|Wrap|Line Numbers
  1. $filtered = array();
  2. foreach( $values as $entry )
  3. {
  4.   parse_str(str_replace(', ', '&', $entry), $index);
  5.  
  6.   $filtered[$index['OU']][$index['CN']] = array( ... );
  7. }
  8.  
Hm. That won't work for the multiple CN values.

The alternative would be to explode() by ', ' and then search through the resulting array:

Expand|Select|Wrap|Line Numbers
  1. $filtered = array();
  2. foreach( $values as $entry )
  3. {
  4.   $split = explode(', ', $entry);
  5.  
  6.   $index = array();
  7.   foreach( $split as $record )
  8.   {
  9.     $keyval = explode('=', $record, 2);
  10.  
  11.     $index[$keyval[0]][] = $keyval[1];
  12.   }
  13.  
  14.   $crawler =& $filtered[$index['OU']];
  15.  
  16.   foreach( $index['CN'] as $group )
  17.   {
  18.     $crawler = array();
  19.     $crawler =& $crawler[$group];
  20.   }
  21.  
  22.   /** Never leave a pointer lying around with the safety off.... */
  23.   unset($crawler);
  24. }
  25.  
Sep 25 '08 #2

100+
P: 107
hmmm, I see where you are going but this didnt quite work for me. Ive tried a few different ways now with recursive functions and all sorts.


Can anyone help me do it this way, first I get a list of the groups, each group is an array of the various parts...

Expand|Select|Wrap|Line Numbers
  1. Array ( [DC=com] => 0 [DC=mydomain] => 1 [CN=Users] => 2 [CN=DHCP Users] => 3 )
  2. Array ( [DC=com] => 0 [DC=mydomain] => 1 [CN=Users] => 2 [CN=A group] => 3 ) 
  3.  
  4.  
Next I want to merge the arrays...

array_merge seems to just take all the values of both arrays and put them into a single layered array like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Array
  3. (
  4.     [DC=com] => 0
  5.     [DC=mydomain] => 1
  6.     [CN=Users] => 2
  7.     [CN=DHCP Users] => 3
  8.     [CN=A group] => 3
  9. )
  10.  
  11.  

But i want them to merge and keep the structure like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. ([DC=com] => (
  3.         [DC=mydomain] => (
  4.                     [CN=Users] => (
  5.                             [CN=DHCP Users] => 3
  6.                             [CN=A group] => 3
  7.                             )
  8.                         )
  9.  
  10.         )
  11. )
  12.  
  13.  
its just kind of a variant of array_merge im looking for i guess.

Andy
Sep 25 '08 #3

ak1dnar
Expert 100+
P: 1,584
Hi,
I changed the thread title bit.
"funny array sort/merge" was little bit funny.
Sep 25 '08 #4

Post your reply

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