What is best way to spit numbers into an array?

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

    #1

    What is best way to spit numbers into an array?

    Looking for a function to split numbers into an array for use in a simple
    graphical counter.

    Objective:
    $count = 12345
    //want to end up with
    $array[0]=1
    $array[1]=2
    $array[2]=3 //and so on then I can do this
    $size=sizeof($a rray);
    for ($r = 0; $r == $size; $r++) {
    echo "<img border='0' src='_borders/$array[$r].gif'> ";
    }

    Note am using PHP4
    Regards
    Dynamo

  • Alan Little

    #2
    Re: What is best way to spit numbers into an array?

    Carved in mystic runes upon the very living rock, the last words of
    Dynamo of comp.lang.php make plain:
    [color=blue]
    > Objective:
    > $count = 12345
    > //want to end up with
    > $array[0]=1
    > $array[1]=2
    > $array[2]=3 //and so on then I can do this[/color]

    $array = explode(' ', trim(chunk_spli t($count, 1, ' ')));

    --
    Alan Little
    Phorm PHP Form Processor

    Comment

    • Oliver Spiesshofer

      #3
      Re: What is best way to spit numbers into an array?

      Dynamo <Dynamo_member@ newsguy.com> wrote in
      news:cp48bg02uo r@drn.newsguy.c om:
      [color=blue]
      > Looking for a function to split numbers into an array for use in a simple
      > graphical counter.
      >
      > Objective:
      > $count = 12345
      > //want to end up with
      > $array[0]=1
      > $array[1]=2
      > $array[2]=3 //and so on then I can do this
      > $size=sizeof($a rray);
      > for ($r = 0; $r == $size; $r++) {
      > echo "<img border='0' src='_borders/$array[$r].gif'> ";
      > }[/color]

      Maybe just do

      for ($r=0;$r<strlen ($count);$r++){
      echo "<img border='0' src='_borders/". substr($count,$ r,1) .".gif'> ";
      }

      or do you _need_ an array?

      Oliver

      Comment

      • Steve

        #4
        Re: What is best way to spit numbers into an array?

        Do this:

        $count = "12345";

        $array = array();

        for ($i=0; $i<strlen($coun t); $i++) {
        $array[] = $count[$i];
        }




        I believe this will work. You can access PHP strings as if it were an
        array. I could be wrong, though, because I never actually had to do it.

        Comment

        • Steve

          #5
          Re: What is best way to spit numbers into an array?

          Do this:

          $count = "12345";

          $array = array();

          for ($i=0; $i<strlen($coun t); $i++) {
          $array[] = $count[$i];
          }




          I believe this will work. You can access PHP strings as if it were an
          array. I could be wrong, though, because I never actually had to do it.

          Comment

          • Dynamo

            #6
            Re: What is best way to spit numbers into an array?

            In article <Xns95B8910E0D5 65oliveremailco m@63.223.5.251> , Oliver Spiesshofer
            says...[color=blue]
            >
            >Dynamo <Dynamo_member@ newsguy.com> wrote in
            >news:cp48bg02u or@drn.newsguy. com:
            >[color=green]
            >> Looking for a function to split numbers into an array for use in a simple
            >> graphical counter.
            >>
            >> Objective:
            >> $count = 12345
            >> //want to end up with
            >> $array[0]=1
            >> $array[1]=2
            >> $array[2]=3 //and so on then I can do this
            >> $size=sizeof($a rray);
            >> for ($r = 0; $r == $size; $r++) {
            >> echo "<img border='0' src='_borders/$array[$r].gif'> ";
            >> }[/color]
            >
            >Maybe just do
            >
            >for ($r=0;$r<strlen ($count);$r++){
            >echo "<img border='0' src='_borders/". substr($count,$ r,1) .".gif'> ";
            >}
            >
            >or do you _need_ an array?[/color]

            No I don't necessarily need an array and your solution did the trick. Many
            thanks.

            Comment

            • Dynamo

              #7
              Re: What is best way to spit numbers into an array?

              In article <Xns95B853B8621 AFalanphormcom@ 216.196.97.132> , Alan Little says...[color=blue]
              >
              >Carved in mystic runes upon the very living rock, the last words of
              >Dynamo of comp.lang.php make plain:
              >[color=green]
              >> Objective:
              >> $count = 12345
              >> //want to end up with
              >> $array[0]=1
              >> $array[1]=2
              >> $array[2]=3 //and so on then I can do this[/color]
              >
              >$array = explode(' ', trim(chunk_spli t($count, 1, ' ')));
              >[/color]

              Thanks but Olivers solution worked without me having to use an array. However, I
              will log it in my "Unused but I'm sure I'll need it in the future" folder. :)

              Comment

              • Alan Little

                #8
                Re: What is best way to spit numbers into an array?

                Carved in mystic runes upon the very living rock, the last words of
                Dynamo of comp.lang.php make plain:
                [color=blue]
                > Thanks but Olivers solution worked without me having to use an array.
                > However, I will log it in my "Unused but I'm sure I'll need it in the
                > future" folder. :)[/color]

                Ah - silly me, answering your question rather than what you were trying to
                do. :)

                --
                Alan Little
                Phorm PHP Form Processor

                Comment

                • Michael Fesser

                  #9
                  Re: What is best way to spit numbers into an array?

                  .oO(Oliver Spiesshofer)
                  [color=blue]
                  >Maybe just do
                  >
                  >for ($r=0;$r<strlen ($count);$r++){
                  >echo "<img border='0' src='_borders/". substr($count,$ r,1) .".gif'> ";
                  >}[/color]

                  Or

                  for ($r = 0; $r < strlen($count); $r++) {
                  echo "<img src='_borders/{$count{$r}}.gi f' alt='{$count{$r }}'>";
                  }

                  Micha

                  Comment

                  Working...