Explode into an array not beginning at 0 ?

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

    Explode into an array not beginning at 0 ?

    I would like to Explode a string into an array that does not begin at
    0 but I can't get it to work.

    For example: $MyInfo = array(1 =27,68,31,19,40 );
    will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40

    But, I'd like to process a string: "27,68,31,19,40 " such that it would
    get the same result.

    I tried:
    $MyString = "27,68,31,19,40 "
    $MyInfo = array(1 =$MyString);
    But that doesn't work.

    I also tried:
    $MyString = "27,68,31,19,40 "
    $MyInfo = array(1 =explode($MyStr ing, ","));
    That doesn't do it either.

    How can this be done?


  • Captain Paralytic

    #2
    Re: Explode into an array not beginning at 0 ?

    On 11 Feb, 16:02, Jack <ironwoodcan... @gmail.comwrote :
    I would like to Explode a string into an array that does not begin at
    0 but I can't get it to work.
    >
    For example: $MyInfo = array(1 =27,68,31,19,40 );
    will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
    >
    But, I'd like to process a string: "27,68,31,19,40 " such that it would
    get the same result.
    >
    I tried:
    $MyString = "27,68,31,19,40 "
    $MyInfo = array(1 =$MyString);
    But that doesn't work.
    >
    I also tried:
    $MyString = "27,68,31,19,40 "
    $MyInfo = array(1 =explode($MyStr ing, ","));
    That doesn't do it either.
    >
    How can this be done?
    It might help if you tell us why you feel the need to do this.

    Comment

    • Rik Wasmus

      #3
      Re: Explode into an array not beginning at 0 ?

      On Mon, 11 Feb 2008 17:02:36 +0100, Jack <ironwoodcanyon @gmail.comwrote :
      I would like to Explode a string into an array that does not begin at
      0 but I can't get it to work.
      >
      For example: $MyInfo = array(1 =27,68,31,19,40 );
      will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
      >
      But, I'd like to process a string: "27,68,31,19,40 " such that it would
      get the same result.
      >
      I tried:
      $MyString = "27,68,31,19,40 "
      $MyInfo = array(1 =$MyString);
      But that doesn't work.
      >
      I also tried:
      $MyString = "27,68,31,19,40 "
      $MyInfo = array(1 =explode($MyStr ing, ","));
      That doesn't do it either.
      >
      How can this be done?

      You shouldn't want it, but here you go:
      Easiest:
      <?php
      $MyString = "27,68,31,19,40 "
      $MyArray = explode(',', '0,'.$MyString) ;
      unset($MyArray[0]);
      ?>
      More verbose:
      <?php
      $MyString = "27,68,31,19,40 "
      $MyArray = explode(',',$My String);
      array_unshift($ MyArray,null);
      unset($MyArray[0]);
      ?>
      --
      Rik Wasmus

      Comment

      • Jack

        #4
        Re: Explode into an array not beginning at 0 ?

        On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic
        <paul_lautman@y ahoo.comwrote:
        >On 11 Feb, 16:02, Jack <ironwoodcan... @gmail.comwrote :
        >I would like to Explode a string into an array that does not begin at
        >0 but I can't get it to work.
        >>
        >For example: $MyInfo = array(1 =27,68,31,19,40 );
        >will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
        >>
        >But, I'd like to process a string: "27,68,31,19,40 " such that it would
        >get the same result.
        >>
        >I tried:
        >$MyString = "27,68,31,19,40 "
        >$MyInfo = array(1 =$MyString);
        >But that doesn't work.
        >>
        >I also tried:
        >$MyString = "27,68,31,19,40 "
        >$MyInfo = array(1 =explode($MyStr ing, ","));
        >That doesn't do it either.
        >>
        >How can this be done?
        >
        >It might help if you tell us why you feel the need to do this.
        How does that impact the the issue of "how can this be done" ?

        Comment

        • Rik Wasmus

          #5
          Re: Explode into an array not beginning at 0 ?

          On Mon, 11 Feb 2008 17:13:09 +0100, Jack <ironwoodcanyon @gmail.comwrote :
          On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic
          <paul_lautman@y ahoo.comwrote:
          >
          >On 11 Feb, 16:02, Jack <ironwoodcan... @gmail.comwrote :
          >>I would like to Explode a string into an array that does not begin at
          >>0 but I can't get it to work.
          >>>
          >>For example: $MyInfo = array(1 =27,68,31,19,40 );
          >>will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
          >>>
          >>But, I'd like to process a string: "27,68,31,19,40 " such that it would
          >>get the same result.
          >>>
          >>I tried:
          >>$MyString = "27,68,31,19,40 "
          >>$MyInfo = array(1 =$MyString);
          >>But that doesn't work.
          >>>
          >>I also tried:
          >>$MyString = "27,68,31,19,40 "
          >>$MyInfo = array(1 =explode($MyStr ing, ","));
          >>That doesn't do it either.
          >>>
          >>How can this be done?
          >>
          >It might help if you tell us why you feel the need to do this.
          >
          How does that impact the the issue of "how can this be done" ?
          We might offer a better solution to the problem then one that will throw
          other/fellow/future coders off balance.
          --
          Rik Wasmus

          Comment

          • Michael Fesser

            #6
            Re: Explode into an array not beginning at 0 ?

            ..oO(Jack)
            >On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic
            ><paul_lautman@ yahoo.comwrote:
            >
            >>It might help if you tell us why you feel the need to do this.
            >
            >How does that impact the the issue of "how can this be done" ?
            It looks as if you just want the indexing to start at 1 instead of 0.
            But usually this is not necessary, since you can always adjust it as
            necessary when accessing the array, for example by giving another start
            value for a loop or by using something like $yourArray[$yourIndex-1].

            Arrays in PHP are always zero-based, many array handling functions
            expect or return them this way.

            So, the question "Why?" is valid.

            Micha

            Comment

            • ZeldorBlat

              #7
              Re: Explode into an array not beginning at 0 ?

              On Feb 11, 11:13 am, Jack <ironwoodcan... @gmail.comwrote :
              On Mon, 11 Feb 2008 08:05:47 -0800 (PST), Captain Paralytic
              >
              >
              >
              <paul_laut...@y ahoo.comwrote:
              On 11 Feb, 16:02, Jack <ironwoodcan... @gmail.comwrote :
              I would like to Explode a string into an array that does not begin at
              0 but I can't get it to work.
              >
              For example: $MyInfo = array(1 =27,68,31,19,40 );
              will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
              >
              But, I'd like to process a string: "27,68,31,19,40 " such that it would
              get the same result.
              >
              I tried:
              $MyString = "27,68,31,19,40 "
              $MyInfo = array(1 =$MyString);
              But that doesn't work.
              >
              I also tried:
              $MyString = "27,68,31,19,40 "
              $MyInfo = array(1 =explode($MyStr ing, ","));
              That doesn't do it either.
              >
              How can this be done?
              >
              It might help if you tell us why you feel the need to do this.
              >
              How does that impact the the issue of "how can this be done" ?
              Because, according to "How To Ask Questions the Smart Way" you should
              describe the /goal/ you are after, not the intermediate step you might
              be stuck on:

              <http://catb.org/~esr/faqs/smart-questions.html# goal>

              Comment

              • Betikci Boris

                #8
                Re: Explode into an array not beginning at 0 ?

                On Feb 11, 6:02 pm, Jack <ironwoodcan... @gmail.comwrote :
                I would like to Explode a string into an array that does not begin at
                0 but I can't get it to work.
                >
                For example: $MyInfo = array(1 =27,68,31,19,40 );
                will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
                >
                But, I'd like to process a string: "27,68,31,19,40 " such that it would
                get the same result.
                >
                I tried:
                $MyString = "27,68,31,19,40 "
                $MyInfo = array(1 =$MyString);
                But that doesn't work.
                >
                I also tried:
                $MyString = "27,68,31,19,40 "
                $MyInfo = array(1 =explode($MyStr ing, ","));
                That doesn't do it either.
                >
                How can this be done?
                -------------
                <?
                $MyString = "27,68,31,19,40 ";
                $MyArray=explod e(",",$MyString );
                for($a=0;$a<siz eof($MyArray);$ a++)
                echo $MyArray[$a] . " ";
                ?>

                Comment

                • Betikci Boris

                  #9
                  Re: Explode into an array not beginning at 0 ?

                  On Feb 12, 12:12 pm, Betikci Boris <pard...@gmail. comwrote:
                  On Feb 11, 6:02 pm, Jack <ironwoodcan... @gmail.comwrote :
                  >
                  I would like to Explode a string into an array that does not begin at
                  0 but I can't get it to work.
                  >
                  For example: $MyInfo = array(1 =27,68,31,19,40 );
                  will result in $MyInfo[1] = 27 ... $MyInfo[5] = 40
                  >
                  But, I'd like to process a string: "27,68,31,19,40 " such that it would
                  get the same result.
                  >
                  I tried:
                  $MyString = "27,68,31,19,40 "
                  $MyInfo = array(1 =$MyString);
                  But that doesn't work.
                  >
                  I also tried:
                  $MyString = "27,68,31,19,40 "
                  $MyInfo = array(1 =explode($MyStr ing, ","));
                  That doesn't do it either.
                  >
                  How can this be done?
                  >
                  -------------
                  <?
                  $MyString = "27,68,31,19,40 ";
                  $MyArray=explod e(",",$MyString );
                  for($a=0;$a<siz eof($MyArray);$ a++)
                  echo $MyArray[$a] . " ";
                  ?>
                  <?
                  $MyString = "27,68,31,19,40 ";
                  $MyArray=explod e(",",$MyString );
                  for($a=0;$a<siz eof($MyArray);$ a++)
                  $MyTempArray[$a+1]=$MyArray[$a];

                  for($a=0;$a<siz eof($MyArray);$ a++)
                  $MyArray[$a]=$MyTempArray[$a];

                  echo $MyArray[1] //Output would be 27;
                  ?>

                  Comment

                  Working...