Query on explode()

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

    Query on explode()

    Hi All,

    Have a look at the following code

    $test = explode('-','one-two-three-four-five');
    print_r($test);

    Output:
    Array
    (
    [0] =one
    [1] =two
    [2] =three
    [3] =four
    [4] =five
    )

    But What I need is

    Array
    (
    [one] =one
    [two] =two
    [three] =three
    [four] =four
    [five] =five
    )

    Any Help Please........

    regards
    moses

  • Janwillem Borleffs

    #2
    Re: Query on explode()

    mosesdinakaran@ gmail.com schreef:
    But What I need is
    >
    Array
    (
    [one] =one
    [two] =two
    [three] =three
    [four] =four
    [five] =five
    )
    >
    For PHP5, you can use array_combine:

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


    For PHP4, have a look at the user contributions on that page.


    JW

    Comment

    • NurAzije

      #3
      Re: Query on explode()

      Try this one, this will work :
      ---------------------------------------------------
      $test = explode('-','one-two-three-four-five');
      $view = new Array();
      foreach($test as $boo =$foo){
      $view[$boo] = $foo;
      }
      print_r($view);
      ------------------------------------------------------

      For php/ajax/javascript tutorials and tips, visit me on my blog at


      Comment

      • .:[ ikciu ]:.

        #4
        Re: Query on explode()

        Hmm NurAzije <nurazije@gmail .comwrote:
        Try this one, this will work :
        ---------------------------------------------------
        $test = explode('-','one-two-three-four-five');
        $view = new Array();
        foreach($test as $boo =$foo){
        $view[$boo] = $foo;
        }
        print_r($view);

        heheheh

        i can write your solution a bit simpler

        $view = $test;

        DONE - but your solution is wrong


        correct solution:
        $view = array_combine($ test, $test);

        --
        ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
        Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

        2be || !2be $this =mysql_query();


        Comment

        • NurAzije

          #5
          Re: Query on explode()

          Try this one, this will work :
          ---------------------------------------------------
          $test = explode('-','one-two-three-four-five');
          $view = new Array();
          foreach($test as $boo =$foo){
          $view[$foo] = $foo; // typing error, do it like this
          }

          print_r($view);
          ------------------------------------------------------

          For php/ajax/javascript tutorials and tips, visit me on my blog at


          Comment

          • .:[ ikciu ]:.

            #6
            Re: Query on explode()

            Hmm NurAzije <nurazije@gmail .comwrote:
            Try this one, this will work :
            ---------------------------------------------------
            $test = explode('-','one-two-three-four-five');
            $view = new Array();
            foreach($test as $boo =$foo){
            $view[$foo] = $foo; // typing error, do it like this
            }
            >
            print_r($view);
            still too much of lines ... try array_combine


            --
            ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
            Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

            2be || !2be $this =mysql_query();


            Comment

            • Rik

              #7
              Re: Query on explode()

              ..:[ ikciu ]:. wrote:
              Hmm NurAzije <nurazije@gmail .comwrote:
              >Try this one, this will work :
              >---------------------------------------------------
              >$test = explode('-','one-two-three-four-five');
              >$view = new Array();
              >foreach($tes t as $boo =$foo){
              > $view[$foo] = $foo; // typing error, do it like this
              >}
              >>
              >print_r($view) ;
              >
              still too much of lines ... try array_combine
              Which Jan-Willem already said, and stated that it doesn't work on PHP < 5.
              You seem to miss that the poster is trying to give a solution for PHP4.

              About the question why the OP would want an array like this, it's beyond
              me. I cannot see any advantage, but then again, I don't know what he's
              trying to accomplish

              Grtz,
              --
              Rik Wasmus


              Comment

              • .:[ ikciu ]:.

                #8
                Re: Query on explode()

                Hmm Rik <luiheidsgoeroe @hotmail.comwro te:
                Which Jan-Willem already said, and stated that it doesn't work on PHP
                < 5. You seem to miss that the poster is trying to give a solution
                for PHP4.
                ofc, but if some1 didn't write what php does he use i try to answer with
                that php what i actualy use


                --
                ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
                Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

                2be || !2be $this =mysql_query();


                Comment

                • Rik

                  #9
                  Re: Query on explode()

                  ..:[ ikciu ]:. wrote:
                  Hmm Rik <luiheidsgoeroe @hotmail.comwro te:
                  >Which Jan-Willem already said, and stated that it doesn't work on PHP
                  >< 5. You seem to miss that the poster is trying to give a solution
                  >for PHP4.
                  >
                  ofc, but if some1 didn't write what php does he use i try to answer
                  with that php what i actualy use
                  It's someone, I thought...

                  First of all, a lot of hosting companies still use PHP4.
                  Second of all, why do you keep insisting on giving an answer for PHP5,
                  which was already given 2 hours before your first post in the thread, as
                  the very first reply on the OP?

                  Grtz,
                  --
                  Rik Wasmus


                  Comment

                  • NurAzije

                    #10
                    Re: Query on explode()

                    I think he wants to show the world he knows PHP5 ;)
                    ------------------------------------------------------

                    For php/ajax/javascript tutorials and tips, visit me on my blog at


                    Comment

                    Working...