PHP Array Challenge!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Saunders

    #1

    PHP Array Challenge!

    This is an amazing opportunity to win a tin of sardines if you can
    answer this difficult question correctly!

    Question:
    What kind of loop do you need to turn an array like this:

    $notes = array(
    '1:1',
    '1:2',
    '1:3'
    array(
    '2:1',
    '2:2',
    array(
    '3:1',
    '3:2'
    );
    '2:3'
    );
    '1:4'
    array(
    '2:4',
    '2:5'
    )
    );

    into a list like this:

    <ul>
    <li>1:1</li>
    <li>1:2</li>
    <li>1:3</li>
    <ul>
    <li>2:1</li>
    <li>2:2</li>
    <ul>
    <li>3:1</li>
    <li>3:2</li>
    </ul>
    <li>2:3</li>
    </ul>
    <li>1:4</li>
    <ul>
    <li>2:4</li>
    <li>2:5</li>
    </ul>
    </ul>

    Obviously in the real world you would probably never encounter this
    problem but i'm interested to see if anyone can solve it.

    Rules:
    - Code must display every element in all the arrays
    - Code must nest the lists as the arrays are nested
    - Code must be able to adapt to any level of array or list nesting
    - <ul>s and <li>s must be terminated correctly with </ul> and </li>
    - Not allowed to reformat var_dump(),prin t_r() or var_export() you
    cheeky bastards
    - Submissions must be receieved by November 1st and can be direct to
    my email ole_v2@dsl.pipe x.com if you wish to prevent others stealing
    your ideas.

    Suggestion:
    I thought you might be able to use a foreach loop starting like this:
    $parsearray = 'notes';
    foreach ($$parsearray as $key => $value) {};
    and then change the $$parsearray to the name of the nested array as you
    find them and then "continue;" . You would need to use a stack go back to
    the previous array and position once that particular nest was done. May
    or may not be right.

    Winner gets a tin of sardines delievered to their door. Tin will be a
    lot like this one
    http://www.oceanbeauty.com/images/Co...nes%20side.jpg only
    better cause I will have touched it. Code is judged on many areas such
    as how compact, elegant and flexible it is.

    Good Luck and... Go On!

    Ollie
  • Erwin Moller

    #2
    Re: PHP Array Challenge!

    Oliver Saunders wrote:
    [color=blue]
    > This is an amazing opportunity to win a tin of sardines if you can
    > answer this difficult question correctly!
    >
    > Question:
    > What kind of loop do you need to turn an array like this:
    >
    > $notes = array(
    > '1:1',
    > '1:2',
    > '1:3'
    > array(
    > '2:1',
    > '2:2',
    > array(
    > '3:1',
    > '3:2'
    > );
    > '2:3'
    > );
    > '1:4'
    > array(
    > '2:4',
    > '2:5'
    > )
    > );
    >[/color]

    You get an error when you try to create that array.
    It is no valid PHP syntax.
    So the first chalange is: Where to add the commas?
    :-)

    Did I win a can of sardines now?

    Regards,
    Erwin Moller

    Comment

    • Ewoud Dronkert

      #3
      Re: PHP Array Challenge!

      Oliver Saunders wrote:[color=blue]
      > What kind of loop do you need to turn an array like this:
      >
      > $notes = array(
      > '1:1',
      > '1:2',
      > '1:3',
      > array(
      > '2:1',
      > '2:2',
      > array(
      > '3:1',
      > '3:2'
      > ),
      > '2:3'
      > ),
      > '1:4',
      > array(
      > '2:4',
      > '2:5'
      > )
      > );
      >
      > into a list like this:
      >
      > <ul>
      > <li>1:1</li>
      > <li>1:2</li>
      > <li>1:3</li>
      > <ul>
      > <li>2:1</li>
      > <li>2:2</li>
      > <ul>
      > <li>3:1</li>
      > <li>3:2</li>
      > </ul>
      > <li>2:3</li>
      > </ul>
      > <li>1:4</li>
      > <ul>
      > <li>2:4</li>
      > <li>2:5</li>
      > </ul>
      > </ul>[/color]

      Like Erwin noticed, the code wasn't correct. I've updated it re. commas
      and semicolons.

      function makelist($a)
      {
      static $level = -1;

      ++$level;
      $spc = str_repeat(' ', 2 * $level);
      $html = "$spc<ul>\n ";
      foreach ( $a as $i )
      {
      if ( is_array($i) )
      $html .= makelist($i);
      else
      $html .= "$spc <li>$i</li>\n";
      }
      $html .= "$spc</ul>\n";

      --$level;
      return $html;
      }

      echo makelist($notes ));

      --
      E. Dronkert

      Comment

      • Oliver Saunders

        #4
        Re: PHP Array Challenge!

        > You get an error when you try to create that array.[color=blue]
        > It is no valid PHP syntax.
        > So the first chalange is: Where to add the commas?
        > :-)
        >
        > Did I win a can of sardines now?
        >
        > Regards,
        > Erwin Moller[/color]

        err..no sorry.
        i didn't test it mainly because i wrote that whole post and then my mail
        client crashed to i had to write it again. so i did it quicker.

        Comment

        • Ewoud Dronkert

          #5
          Re: PHP Array Challenge!

          > echo makelist($notes ));

          Sorry, extra parenthesis left over from htmlspecialchar s().

          echo makelist($notes );

          --
          E. Dronkert

          Comment

          • Oliver Saunders

            #6
            Re: PHP Array Challenge!

            > function makelist($a)[color=blue]
            > {
            > static $level = -1;
            >
            > ++$level;
            > $spc = str_repeat(' ', 2 * $level);
            > $html = "$spc<ul>\n ";
            > foreach ( $a as $i )
            > {
            > if ( is_array($i) )
            > $html .= makelist($i);
            > else
            > $html .= "$spc <li>$i</li>\n";
            > }
            > $html .= "$spc</ul>\n";
            >
            > --$level;
            > return $html;
            > }
            >
            > echo makelist($notes ));[/color]

            Shame about the extra parentesis on your last line. But i'm willing to
            let that go.

            The solution is elegant, flexible, compact and I especially like the way
            you put in the whitespace as well. You, my friend, have won yourself a
            can of sardines and my respect as an excellent PHP programmer. Well Done!

            What's the address you would like them delievered then?

            Comment

            • Ewoud Dronkert

              #7
              Re: PHP Array Challenge!

              Oliver Saunders wrote:[color=blue]
              > Shame about the extra parentesis on your last line. But i'm willing to
              > let that go.[/color]

              Woohoo! :D
              [color=blue]
              > The solution is elegant, flexible, compact and I especially like the way
              > you put in the whitespace as well. You, my friend, have won yourself a
              > can of sardines and my respect as an excellent PHP programmer. Well Done!
              >
              > What's the address you would like them delievered then?[/color]

              Thanks man. I'm in Europe (Netherlands) so that's a bit out of the way.
              I'll donate the tin to your next winner.

              --
              E. Dronkert

              Comment

              • Oliver Saunders

                #8
                Re: PHP Array Challenge!

                > Woohoo! :D

                :)
                [color=blue]
                > Thanks man. I'm in Europe (Netherlands) so that's a bit out of the way.
                > I'll donate the tin to your next winner.[/color]

                lol, thanks I appreciate that. I've got to think of a harder challenge now.

                Comment

                • Ed

                  #9
                  Re: PHP Array Challenge!

                  Oliver Saunders wrote:[color=blue]
                  > This is an amazing opportunity to win a tin of sardines if you can
                  > answer this difficult question correctly!
                  >
                  > Question:
                  > What kind of loop do you need to turn an array like this:
                  >
                  > $notes = array(
                  > '1:1',
                  > '1:2',
                  > '1:3'
                  > array(
                  > '2:1',
                  > '2:2',
                  > array(
                  > '3:1',
                  > '3:2'
                  > );
                  > '2:3'
                  > );
                  > '1:4'
                  > array(
                  > '2:4',
                  > '2:5'
                  > )
                  > );
                  >
                  > into a list like this:
                  >
                  > <ul>
                  > <li>1:1</li>
                  > <li>1:2</li>
                  > <li>1:3</li>
                  > <ul>
                  > <li>2:1</li>
                  > <li>2:2</li>
                  > <ul>
                  > <li>3:1</li>
                  > <li>3:2</li>
                  > </ul>
                  > <li>2:3</li>
                  > </ul>
                  > <li>1:4</li>
                  > <ul>
                  > <li>2:4</li>
                  > <li>2:5</li>
                  > </ul>
                  > </ul>[/color]

                  FYI - The way you have nested the lists is invalid. A nested list can
                  only be inside another list element :)

                  Ed

                  ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                  http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                  ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                  Comment

                  • Ewoud Dronkert

                    #10
                    Re: PHP Array Challenge!

                    Ed wrote:[color=blue]
                    > FYI - The way you have nested the lists is invalid. A nested list can
                    > only be inside another list element :)[/color]

                    Damn, that's right! Then how about:

                    function makelist($a)
                    {
                    static $level = 0;
                    $open = FALSE;

                    ++$level;
                    $tab = str_repeat("\t" , $level);

                    $html = "<ul>\n";
                    foreach ( $a as $item )
                    {
                    if ( is_array($item) )
                    $html .= makelist($item) ;
                    else
                    {
                    if ( $open )
                    $html .= "</li>\n";
                    else
                    $open = TRUE;
                    $html .= "$tab<li>$item" ;
                    }
                    }
                    if ( $open )
                    $html .= "</li>";
                    $html .= "</ul>";

                    --$level;
                    return $html;
                    }

                    --
                    E. Dronkert

                    Comment

                    Working...