Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:03:29+00:00 2026-05-27T05:03:29+00:00

Ok i’ve to admin i’m not a master in recursion. This could be kinda

  • 0

Ok i’ve to admin i’m not a master in recursion. This could be kinda a newbie problem.

What i’m trying to do is to compute the section number. As you can see as you go in depth the section number changes in a predictable way:

multipart/mixed
 -- (part 0, sec. "1") multipart/related
    -- (part 0, sec. "1.1") multipart/alternative
       -- (part 0, sec. "1.1.1") text/plain
       -- (part 1, sec. "1.1.2") text/html
    -- (part 1, sec. "1.2") image/gif
 -- (part 1, sec. "2") image/png

That is, section number is simply the part number (plus 1) followed by a dot and again by the part number (plus 1), how many times depends on nesting level.

A simple $struct to pass to parse() function is like:

object(stdClass)
   public 'type' => int // if 1 it's multipart
   public 'parts' => array // Inner parts

While my function is quite like this:

public function parse($struct, $depth = '')
{
   if(!isset($struct->parts)) return; // Base case of recursion: no parts inside

   // $struct->parts is array: index starting from 0.
   for($i = 0, $j = count($struct->parts); $i < $j; $i++)
   {
      $part = $struct->parts[$i]; // Current part
      $ptno = $i + 1; // This is the part number, will be used to build $secno

      // Multipart? Go further in recursion passing the new level of nesting
      if($part->type == 1) $this->parse($part, $depth .= "$partno" . ".");

      // Compute the section number with the given $depth (if any)
      // ACTUALLY NOT WORKING
      $secno = !empty($depth) ? "$depth$ptno" : "$ptno";

      // Where am i?
      echo self::$TYPES[$part->type] . '/' . $part->subtype . ": $secno<br/>";
   }
}

Output (wrong):

text/PLAIN: 1.1.1
text/HTML: 1.1.2
multipart/ALTERNATIVE: 1.1.1
image/GIF: 1.1.2
multipart/RELATED: 1.1
image/PNG: 1.2

This is how it should be:

text/PLAIN: 1.1.1
text/HTML: 1.1.2
multipart/ALTERNATIVE: 1.1
image/GIF: 1.2
multipart/RELATED: 1
image/PNG: 2

EDIT: copy & paste test data:

$test = (object) array(
    'type' => 1, // multipart
    'subtype' => 'MIXED',
    'parts' => array(
        (object) array(
            'type'    => 1, // multipart
            'subtype' => 'RELATED',
            'parts'   => array(
                (object) array(
                    'type'    => 1, // multipart
                    'subtype' => 'ALTERNATIVE',
                    'parts'   => array(
                        (object) array('type' => 0, 'subtype' => 'PLAIN'),
                        (object) array('type' => 0, 'subtype' => 'HTML'),
                    )
                ),
                (object) array(
                    'type'    => 5, // image
                    'subtype' => 'GIF'
                )
            )
        ),
        (object) array(
            'type'    => 5, // image
            'subtype' => 'PNG'
        )
    )
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:03:30+00:00Added an answer on May 27, 2026 at 5:03 am

    The problem is you are .= appending $partno when you should just be . appending here:

    $this->parse($part, $depth .= "$partno" . ".");
    

    So fixing that in the context of your code, you get:

    public function parse($struct, $depth = '')
    {
       if(!isset($struct->parts)) return; // Base case of recursion: no parts inside
    
       // $struct->parts is array: index starting from 0.
       for($i = 0, $j = count($struct->parts); $i < $j; $i++)
       {
          $part = $struct->parts[$i]; // Current part
          $ptno = $i + 1; // This is the part number, will be used to build $secno
    
          // Multipart? Go further in recursion passing the new level of nesting
          if($part->type == 1) $this->parse($part, $depth . "$partno" . ".");
    
          // Compute the section number with the given $depth (if any)
          $secno = !empty($depth) ? "$depth$ptno" : "$ptno";
    
          // Where am i?
          echo self::$TYPES[$part->type] . '/' . $part->subtype . ": $secno<br/>";
       }
    }
    

    And you can restructure your code like this to avoid code duplication more:

    public function parse($struct, $depth = '')
    {
       if(!isset($struct->parts)) return; // Base case of recursion: no parts inside
    
       // $struct->parts is array: index starting from 0.
       for($i = 0, $j = count($struct->parts); $i < $j; $i++)
       {
          $part = $struct->parts[$i]; // Current part
          $ptno = $i + 1; // This is the part number, will be used to build $secno
    
          // Compute the section number with the given $depth (if any)
          $secno = $depth . $ptno
    
          // Multipart? Go further in recursion passing the new level of nesting
          if($part->type == 1) $this->parse($part, $secno . '.');
    
          // Where am i?
          echo self::$TYPES[$part->type] . '/' . $part->subtype . ": $secno<br/>";
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.