inserting elements within an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    inserting elements within an array

    Hi,

    Using PHP 4.4.4, I have this array

    $my_arr = array("a", "b", "c", "d");

    What is the easiest way to insert the element "|" between the elements
    of the array so that the result is

    array("a", "|", "b", "|", "c", "|", "d");

    ? Thanks, - Dave

  • Hendri Kurniawan

    #2
    Re: inserting elements within an array

    laredotornado@z ipmail.com wrote:
    Hi,
    >
    Using PHP 4.4.4, I have this array
    >
    $my_arr = array("a", "b", "c", "d");
    >
    What is the easiest way to insert the element "|" between the elements
    of the array so that the result is
    >
    array("a", "|", "b", "|", "c", "|", "d");
    >
    ? Thanks, - Dave
    >
    Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
    But someone in this NG bound to find a better way to do it :D

    Hendri Kurniawan

    Comment

    • Geoff Berrow

      #3
      Re: inserting elements within an array

      Message-ID: <132fone41inqp4 b@corp.supernew s.comfrom Hendri Kurniawan
      contained the following:
      >What is the easiest way to insert the element "|" between the elements
      >of the array so that the result is
      >>
      >array("a", "|", "b", "|", "c", "|", "d");
      >>
      >? Thanks, - Dave
      >>
      >
      >Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
      >But someone in this NG bound to find a better way to do it :D
      The question is /why/ does he want to do it?

      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Hendri Kurniawan

        #4
        Re: inserting elements within an array

        Geoff Berrow wrote:
        Message-ID: <132fone41inqp4 b@corp.supernew s.comfrom Hendri Kurniawan
        contained the following:
        >
        >>What is the easiest way to insert the element "|" between the elements
        >>of the array so that the result is
        >>>
        >>array("a", "|", "b", "|", "c", "|", "d");
        >>>
        >>? Thanks, - Dave
        >>>
        >Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
        >But someone in this NG bound to find a better way to do it :D
        >
        The question is /why/ does he want to do it?
        >
        hehehe... yeah... i forgot to ask why.... :D
        Well...

        Hendri Kurniawan

        Comment

        • laredotornado@zipmail.com

          #5
          Re: inserting elements within an array

          On Apr 19, 4:45 pm, Hendri Kurniawan <ask...@email.c omwrote:
          laredotorn...@z ipmail.com wrote:
          Hi,
          >
          Using PHP 4.4.4, I have this array
          >
          $my_arr = array("a", "b", "c", "d");
          >
          What is the easiest way to insert the element "|" between the elements
          of the array so that the result is
          >
          array("a", "|", "b", "|", "c", "|", "d");
          >
          ? Thanks, - Dave
          >
          Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
          But someone in this NG bound to find a better way to do it :D
          >
          Hendri Kurniawan
          Beautiful! Concise and accurate. I have given your post the highest
          rating on Google Groups. - Dave

          Comment

          • FFMG

            #6
            Re: inserting elements within an array


            laredotornado@z ipmail.com;61127 Wrote:
            On Apr 19, 4:45 pm, Hendri Kurniawan <ask...@email.c omwrote:
            laredotorn...@z ipmail.com wrote:
            Hi,
            Using PHP 4.4.4, I have this array
            $my_arr = array("a", "b", "c", "d");
            What is the easiest way to insert the element "|" between the
            elements
            of the array so that the result is
            array("a", "|", "b", "|", "c", "|", "d");
            ? Thanks, - Dave
            Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
            But someone in this NG bound to find a better way to do it :D

            Hendri Kurniawan
            >
            Beautiful! Concise and accurate. I have given your post the highest
            rating on Google Groups. - Dave
            Ah, Google Groups, there goes the 'why' part.

            FFMG


            --

            'webmaster forum' (http://www.httppoint.com) | 'webmaster Directory'
            (http://www.webhostshunter.com/) | 'Recreation Vehicle insurance'
            (http://www.insurance-owl.com/other/car_rec.php)
            'Free URL redirection service' (http://urlkick.com/)
            ------------------------------------------------------------------------
            FFMG's Profile: http://www.httppoint.com/member.php?userid=580
            View this thread: http://www.httppoint.com/showthread.php?t=14026

            Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

            Comment

            • Hendri Kurniawan

              #7
              Re: inserting elements within an array

              FFMG wrote:
              laredotornado@z ipmail.com;61127 Wrote:
              >On Apr 19, 4:45 pm, Hendri Kurniawan <ask...@email.c omwrote:
              >>laredotorn... @zipmail.com wrote:
              >>>Hi,
              >>>Using PHP 4.4.4, I have this array
              >>>$my_arr = array("a", "b", "c", "d");
              >>>What is the easiest way to insert the element "|" between the
              >elements
              >>>of the array so that the result is
              >>>array("a", "|", "b", "|", "c", "|", "d");
              >>>? Thanks, - Dave
              >>Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
              >>But someone in this NG bound to find a better way to do it :D
              >>>
              >>Hendri Kurniawan
              >Beautiful! Concise and accurate. I have given your post the highest
              >rating on Google Groups. - Dave
              >
              Ah, Google Groups, there goes the 'why' part.
              >
              FFMG
              >
              >

              ?? why?

              Hendri Kurniawan

              Comment

              • Toby A Inkster

                #8
                Re: inserting elements within an array

                Hendri Kurniawan wrote:
                $my_arr = explode(',', implode(',|,', $my_arr));
                This will work given the example array, but won't in the general case.
                Take for example, the following input:

                $sinatra_songs = array(
                "My Way",
                "New York, New York",
                "Strangers in the Night",
                "Ol' Man River",
                "Something Stupid",
                "Bad, Bad Leroy Brown",
                "That's Life"
                );

                You could work around that by choosing something other than a comma --
                something virtually guaranteed to be unique -- as a divider.

                $u = md5(microtime() .rand(1,1000));
                $my_arr = explode($u, implode("$u|$u" , $my_arr));

                --
                Toby A Inkster BSc (Hons) ARCS
                Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

                Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

                * = I'm getting there!

                Comment

                • Geoff Berrow

                  #9
                  Re: inserting elements within an array

                  Message-ID: <1177043678.956 262.311730@y5g2 000hsa.googlegr oups.comfrom
                  laredotornado@z ipmail.com contained the following:
                  >Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
                  >But someone in this NG bound to find a better way to do it :D
                  >>
                  >Hendri Kurniawan
                  >
                  >Beautiful! Concise and accurate. I have given your post the highest
                  >rating on Google Groups.
                  Why? No one learned anything from this.

                  --
                  Geoff Berrow (put thecat out to email)
                  It's only Usenet, no one dies.
                  My opinions, not the committee's, mine.
                  Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                  Comment

                  • optimistx

                    #10
                    Re: inserting elements within an array

                    Geoff Berrow wrote:
                    ....
                    Why? No one learned anything from this.
                    How do you know that?


                    Comment

                    • Geoff Berrow

                      #11
                      Re: inserting elements within an array

                      Message-ID: <4628a120$0$315 33$f4da826c@new s.24online.fifr om optimistx
                      contained the following:
                      >Why? No one learned anything from this.
                      >
                      >How do you know that?
                      Well the OP now has a bit of code he probably doesn't understand to
                      solve a problem that he hasn't really thought through.

                      Besides, it's very easy just to provide a code snippet in response to a
                      request, but unless you know why the request is being made you could be
                      leading the OP in the wrong direction altogether.

                      --
                      Geoff Berrow (put thecat out to email)
                      It's only Usenet, no one dies.
                      My opinions, not the committee's, mine.
                      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                      Comment

                      • FFMG

                        #12
                        Re: inserting elements within an array


                        Hendri Kurniawan;61133 Wrote:
                        FFMG wrote:
                        laredotornado@z ipmail.com;61127 Wrote:
                        On Apr 19, 4:45 pm, Hendri Kurniawan <ask...@email.c omwrote:
                        >laredotorn...@ zipmail.com wrote:
                        >>Hi,
                        >>Using PHP 4.4.4, I have this array
                        >>$my_arr = array("a", "b", "c", "d");
                        >>What is the easiest way to insert the element "|" between the
                        elements
                        >>of the array so that the result is
                        >>array("a", "|", "b", "|", "c", "|", "d");
                        >>? Thanks, - Dave
                        >Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
                        >But someone in this NG bound to find a better way to do it :D
                        >>
                        >Hendri Kurniawan
                        Beautiful! Concise and accurate. I have given your post the
                        highest
                        rating on Google Groups. - Dave
                        Ah, Google Groups, there goes the 'why' part.

                        FFMG
                        >
                        >
                        ?? why?
                        >
                        Hendri Kurniawan
                        No reason really, just kidding.
                        Just curious as to why the OP wanted to do such an insert.

                        FFMG


                        --

                        'webmaster forum' (http://www.httppoint.com) | 'webmaster Directory'
                        (http://www.webhostshunter.com/) | 'Recreation Vehicle insurance'
                        (http://www.insurance-owl.com/other/car_rec.php)
                        'Free URL redirection service' (http://urlkick.com/)
                        ------------------------------------------------------------------------
                        FFMG's Profile: http://www.httppoint.com/member.php?userid=580
                        View this thread: http://www.httppoint.com/showthread.php?t=14026

                        Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

                        Comment

                        • Hendri Kurniawan

                          #13
                          Re: inserting elements within an array

                          Geoff Berrow wrote:
                          Message-ID: <4628a120$0$315 33$f4da826c@new s.24online.fifr om optimistx
                          contained the following:
                          >
                          >>Why? No one learned anything from this.
                          >How do you know that?
                          >
                          Well the OP now has a bit of code he probably doesn't understand to
                          solve a problem that he hasn't really thought through.
                          >
                          Besides, it's very easy just to provide a code snippet in response to a
                          request, but unless you know why the request is being made you could be
                          leading the OP in the wrong direction altogether.
                          >
                          Point taken....
                          I just don't like typing long and winded sentences...

                          Hendri Kurniawan

                          Comment

                          • bill

                            #14
                            Re: inserting elements within an array

                            Geoff Berrow wrote:
                            Message-ID: <1177043678.956 262.311730@y5g2 000hsa.googlegr oups.comfrom
                            laredotornado@z ipmail.com contained the following:
                            >
                            >>Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
                            >>But someone in this NG bound to find a better way to do it :D
                            >>>
                            >>Hendri Kurniawan
                            >Beautiful! Concise and accurate. I have given your post the highest
                            >rating on Google Groups.
                            >
                            Why? No one learned anything from this.
                            >
                            I learned from this concise example.
                            I can not imagine needing to use this example, but *something*
                            like this might pop up someday and now it is lurking in the back
                            of my mind.

                            bill

                            Comment

                            • Rami Elomaa

                              #15
                              Re: inserting elements within an array

                              Hendri Kurniawan kirjoitti:
                              FFMG wrote:
                              >laredotornado@z ipmail.com;61127 Wrote:
                              >>On Apr 19, 4:45 pm, Hendri Kurniawan <ask...@email.c omwrote:
                              >>>laredotorn.. .@zipmail.com wrote:
                              >>>>Hi,
                              >>>>Using PHP 4.4.4, I have this array
                              >>>>$my_arr = array("a", "b", "c", "d");
                              >>>>What is the easiest way to insert the element "|" between the
                              >>elements
                              >>>>of the array so that the result is
                              >>>>array("a" , "|", "b", "|", "c", "|", "d");
                              >>>>? Thanks, - Dave
                              >>>Easiest, IMHO $my_arr = explode(',', implode(',|,', $my_arr));
                              >>>But someone in this NG bound to find a better way to do it :D
                              >>>>
                              >>>Hendri Kurniawan
                              >>Beautiful! Concise and accurate. I have given your post the highest
                              >>rating on Google Groups. - Dave
                              >>
                              >Ah, Google Groups, there goes the 'why' part.
                              >>
                              >FFMG
                              >>
                              >>
                              >
                              >
                              ?? why?
                              >
                              See Geoff's post: "The question is /why/ does he want to do it?"

                              What FFMG is suggesting is that if a person thinks 'google groups' ==
                              'usenet' then they'll propably be asking silly questions too.

                              --
                              Rami.Elomaa@gma il.com

                              "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
                              usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

                              Comment

                              Working...