Exploding an array, kind of ...

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

    Exploding an array, kind of ...

    I have an array as follows

    $sounds = array();

    $sounds['sound1'] = "fishes/1/sound.php";
    $sounds['sound2'] = "fishes/2/sound.php";
    $sounds['sound3'] = "fishes/3/sound.php";
    $sounds['sound4] = "fishes/4/sound.php";
    $sounds['sound5] = "fishes/5/sound.php";
    $sounds['sound11] = "bears/1/sound.php";

    etc etc......

    What I want to be able to do have turn the above into this:

    include ("sounds/fishes/1/sound.php");
    include ("sounds/fishes/2/sound.php");
    print ("<br>divider<b r>");
    include ("sounds/fishes/3/sound.php");
    include ("sounds/fishes/4/sound.php");
    print ("<br>divider<b r>");
    include ("sounds/fishes/5/sound.php");
    include ("sounds/birds/1/sound.php");

    etc etc

    I'm totally clueless as to how I might acheive it, especially including the
    "dividers" every two includes. Surely one of you clever folk gotta be able
    to spoon feed it to me? ;)



  • Jeffrey Silverman

    #2
    Re: Exploding an array, kind of ...

    On Tue, 23 Mar 2004 16:46:55 +0000, mark wrote:
    [color=blue]
    > I'm totally clueless as to how I might acheive it, especially including the
    > "dividers" every two includes. Surely one of you clever folk gotta be able
    > to spoon feed it to me? ;)[/color]

    no spoon feeding until you try harder. Some pointers, though:

    Use

    foreach($sounds as $key => $value){
    }

    and

    the modulus operator (%) to determine if two rows have passed.


    later....
    --
    Jeffrey D. Silverman | jeffrey AT jhu DOT edu
    Website | http://www.wse.jhu.edu/newtnotes/

    Comment

    • mark

      #3
      Re: Exploding an array, kind of ...

      "Jeffrey Silverman" <jeffrey@jhu.ed u> wrote in message
      news:pan.2004.0 3.23.18.48.21.8 40042@jhu.edu.. .
      [color=blue]
      >
      > no spoon feeding until you try harder. Some pointers, though:
      >
      > Use
      >
      > foreach($sounds as $key => $value){
      > }
      >
      > and
      >
      > the modulus operator (%) to determine if two rows have passed.
      >
      >[/color]

      thanks for the reply, but honestly I'm useless at PHP, any chance you could
      tell me the answer really? pretty please?



      Comment

      • Jay Moore

        #4
        Re: Exploding an array, kind of ...

        mark wrote:
        [color=blue]
        > "Jeffrey Silverman" <jeffrey@jhu.ed u> wrote in message
        > news:pan.2004.0 3.23.18.48.21.8 40042@jhu.edu.. .
        >
        >[color=green]
        >>no spoon feeding until you try harder. Some pointers, though:
        >>
        >>Use
        >>
        >>foreach($soun ds as $key => $value){
        >>}
        >>
        >>and
        >>
        >>the modulus operator (%) to determine if two rows have passed.
        >>
        >>[/color]
        >
        >
        > thanks for the reply, but honestly I'm useless at PHP, any chance you could
        > tell me the answer really? pretty please?
        >
        >
        >[/color]

        You're not gonna get any better at PHP if everyone does all the work for
        you. ;)

        The foreach() will loop thru your $sounds array and pull the key/value
        pairs and assign them to $key and $value. Then it's up to you to do
        whatever you want with the two variables.

        For the modulus (remainder of division), you could use a counter and
        check if the counter is evenly divisible by 2 ($ctr % 2). If it is,
        output your image/text/hr whatever. If not, continue the loop.

        -Jay

        Comment

        • mark

          #5
          Re: Exploding an array, kind of ...

          "Jay Moore" <address@isp.tl d> wrote in message
          news:Tg38c.2794 8$4B1.6481@twis ter.rdc-kc.rr.com...[color=blue]
          > mark wrote:[/color]
          [color=blue]
          > You're not gonna get any better at PHP if everyone does all the work for
          > you. ;)
          >
          > The foreach() will loop thru your $sounds array and pull the key/value
          > pairs and assign them to $key and $value. Then it's up to you to do
          > whatever you want with the two variables.
          >
          > For the modulus (remainder of division), you could use a counter and
          > check if the counter is evenly divisible by 2 ($ctr % 2). If it is,
          > output your image/text/hr whatever. If not, continue the loop.
          >[/color]

          Excellent! I got it now and it works :) many thanks....

          You don't happen to know how I can have the array come out upside down do
          you too? pretty pretty please?


          Comment

          • Pedro Graca

            #6
            Re: Exploding an array, kind of ...

            mark wrote (edited):[color=blue]
            > How can I have the array come out upside down?[/color]

            PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


            Also look at all the other array functions. They're wonderful :)
            --
            USENET would be a better place if everybody read: : mail address :
            http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
            http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
            http://www.expita.com/nomime.html : to 10K bytes :

            Comment

            • mark

              #7
              Re: Exploding an array, kind of ...


              "Pedro Graca" <hexkid@hotpop. com> wrote in message
              news:c3qhic$2bg thg$3@ID-203069.news.uni-berlin.de...[color=blue]
              > mark wrote (edited):[color=green]
              > > How can I have the array come out upside down?[/color]
              >
              > http://www.php.net/array_reverse
              >
              > Also look at all the other array functions. They're wonderful :)[/color]

              thanks, If i try array_rand or revervse I get:

              Warning: Wrong parameter count for array_rand on line 3:

              :/


              Comment

              • Jay Moore

                #8
                Re: Exploding an array, kind of ...



                mark wrote:
                [color=blue]
                > "Jay Moore" <address@isp.tl d> wrote in message
                > news:Tg38c.2794 8$4B1.6481@twis ter.rdc-kc.rr.com...
                >[color=green]
                >>mark wrote:[/color]
                >
                >[color=green]
                >>You're not gonna get any better at PHP if everyone does all the work for
                >>you. ;)
                >>
                >>The foreach() will loop thru your $sounds array and pull the key/value
                >>pairs and assign them to $key and $value. Then it's up to you to do
                >>whatever you want with the two variables.
                >>
                >>For the modulus (remainder of division), you could use a counter and
                >>check if the counter is evenly divisible by 2 ($ctr % 2). If it is,
                >>output your image/text/hr whatever. If not, continue the loop.
                >>[/color]
                >
                >
                > Excellent! I got it now and it works :) many thanks....
                >
                > You don't happen to know how I can have the array come out upside down do
                > you too? pretty pretty please?
                >
                >[/color]

                PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

                PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


                Comment

                • Pedro Graca

                  #9
                  Re: Exploding an array, kind of ...

                  mark wrote:[color=blue]
                  > thanks, If i try array_rand or revervse I get:
                  >
                  > Warning: Wrong parameter count for array_rand on line 3:[/color]

                  Post your code here. For me, both functions work as specified in the
                  manual.

                  --
                  USENET would be a better place if everybody read: : mail address :
                  http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
                  http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
                  http://www.expita.com/nomime.html : to 10K bytes :

                  Comment

                  • mark

                    #10
                    Re: Exploding an array, kind of ...

                    "Jay Moore" <address@isp.tl d> wrote in message
                    news:1_38c.2795 5$4B1.3155@twis ter.rdc-kc.rr.com...[color=blue]
                    >
                    > http://us2.php.net/manual/en/function.krsort.php
                    >[/color]

                    that works, although I have some problems because my array values are like
                    this 1, 11, 111 instead of 001, 011, 111


                    Comment

                    Working...