Connecting Tech Pros Worldwide Forums | Help | Site Map

Array sort/merge

Member
 
Join Date: Jul 2007
Posts: 107
#1: Sep 24 '08
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

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Sep 25 '08

re: Array sort/merge


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.  
Member
 
Join Date: Jul 2007
Posts: 107
#3: Sep 25 '08

re: Array sort/merge


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
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#4: Sep 25 '08

re: Array sort/merge


Hi,
I changed the thread title bit.
"funny array sort/merge" was little bit funny.
Reply