SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recursion? Tree...

    Hi,

    I'm trying to create an array from a larger array which will get all parent categories up to the top most category.

    In this example the category I want to retrieve the relationships for is 182.

    My query returns the following:

    Code:
    array (
      100 => '0',
      101 => '0',
      102 => '0',
      103 => '0',
      104 => '0',
      107 => '0',
      109 => '107',
      111 => '0',
      131 => '0',
      132 => '0',
      178 => '0',
      179 => '178',
      180 => '179',
      181 => '179',
      182 => '181',
    )
    As the array shows 182 has a parent of 181, 181 has a parent of 179, 179 has a parent of 180 and 180 is the top-most category.

    I'm struggling to write a function which pulls out this relationship from the rest and displays it.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I realise I could call and SQL function on each parent ID but I don't want to do this due to the amount of DB calls that could occur.

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    You could use a recursive function (didn't test it):
    Code PHP:
    function getChain($array, $value) {
      $result = $array[$value] . ' ';
      if ($array[$value] <> '0') {
        $result .= getChain($array, $array[$value]);
      }
    }
     
    $array = array (
      100 => '0',
      101 => '0',
      102 => '0',
      103 => '0',
      104 => '0',
      107 => '0',
      109 => '107',
      111 => '0',
      131 => '0',
      132 => '0',
      178 => '0',
      179 => '178',
      180 => '179',
      181 => '179',
      182 => '181',
    );
    $value = 182;
    echo getChain($array, $value);

  4. #4
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, looks good but how can I return the relationship back into an array? So it returns something like:

    PHP Code:
    $aRelations[182]=181
    $aRelations
    [181]=179
    $aRelations
    [179]=178
    $aRelations
    [178]=

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Code PHP:
    function getChain($array, $value) {
      $result[$value] = $array[$value];
      if ($array[$value] <> '0') {
        $result = $result + getChain($array, $array[$value]);
      }
      return $result
    }
    Forgot the return...

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    I really must remember you can use the addition operator on arrays. Despite seeing it many times, it always seems to be a revelation when I see it again.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Off Topic:

    I really must remember you can use the addition operator on arrays. Despite seeing it many times, it always seems to be a revelation when I see it again.
    Yeah, me too
    I was looking for a way to add a value to the beginning of the array without losing numeric key values (array_unshift), and the second user comment gave me this illuminating tip

  8. #8
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, that works perfect, I'll do some further testing with different scenarios. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •