Array sort/merge

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    Array sort/merge

    hey,

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


    Code:
    myar[0] "CN=my sub group, CN=my group, OU=myou"
    myar[1] "CN=another subgroup, CN=my group, OU=myou"
    myar[3] "CN=diff group, OU=myou"
    myar[4] "CN=foo group, OU=ou2"
    myar[5] "CN=wowgroup, CN=foo group, OU=ou2"

    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:

    Code:
    arr['myou']['mygroup']['my sub group']
    ----------------------['another sub group']
    -----------['diff group']
    arr['ou2']-['foo group']['wowgroup']

    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
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    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:
    [code=php]
    $filtered = array();
    foreach( $values as $entry )
    {
    parse_str(str_r eplace(', ', '&', $entry), $index);

    $filtered[$index['OU']][$index['CN']] = array( ... );
    }
    [/code]

    Hm. That won't work for the multiple CN values.

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

    [code=php]
    $filtered = array();
    foreach( $values as $entry )
    {
    $split = explode(', ', $entry);

    $index = array();
    foreach( $split as $record )
    {
    $keyval = explode('=', $record, 2);

    $index[$keyval[0]][] = $keyval[1];
    }

    $crawler =& $filtered[$index['OU']];

    foreach( $index['CN'] as $group )
    {
    $crawler = array();
    $crawler =& $crawler[$group];
    }

    /** Never leave a pointer lying around with the safety off.... */
    unset($crawler) ;
    }
    [/code]

    Comment

    • theS70RM
      New Member
      • Jul 2007
      • 107

      #3
      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...

      Code:
      Array ( [DC=com] => 0 [DC=mydomain] => 1 [CN=Users] => 2 [CN=DHCP Users] => 3 )
      Array ( [DC=com] => 0 [DC=mydomain] => 1 [CN=Users] => 2 [CN=A group] => 3 )
      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:

      Code:
      Array
      (
          [DC=com] => 0
          [DC=mydomain] => 1
          [CN=Users] => 2
          [CN=DHCP Users] => 3
          [CN=A group] => 3
      )

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

      Code:
      ([DC=com] => (
      		[DC=mydomain] => (
      					[CN=Users] => (
      							[CN=DHCP Users] => 3
      							[CN=A group] => 3
      							)
      						)
      
      		)
      )
      its just kind of a variant of array_merge im looking for i guess.

      Andy

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

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

        Comment

        Working...