Reading commaseparated string into array

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

    #1

    Reading commaseparated string into array

    Hi, I need some help on reading commaseparated strings into arrays as numbers.

    When I use

    $data = "12.4,11.3,13.2 ";
    $myarray = explode(',',$da ta);

    I get strings, not numbers:.

    $myarray:array =
    0 = "12.4"
    1 = "11.3"
    2 = "13.2"

    where I need

    $myarray:array =
    0 = 12.4
    1 = 11.3
    2 = 13.2

    What to do?
  • Ewoud Dronkert

    #2
    Re: Reading commaseparated string into array

    On Mon, 18 Apr 2005 10:58:54 -0400, Martin < > wrote:[color=blue]
    > $data = "12.4,11.3,13.2 ";
    > $myarray = explode(',',$da ta);
    > I get strings, not numbers:.[/color]

    It's probably good enough; whenever you use variables in a numerical
    context, they are treated as numbers, even if they are numbers in a
    string. If you *have* to make them numbers first, try:

    function makefloat( $a )
    {
    return (float)$a;
    }
    $numdata = array_map( 'makefloat', $strdata ); // PHP >= 4.0.6


    --
    Firefox Web Browser - Rediscover the web - http://getffox.com/
    Thunderbird E-mail and Newsgroups - http://gettbird.com/

    Comment

    • Good Man

      #3
      Re: Reading commaseparated string into array

      Ewoud Dronkert <firstname@last name.net.invali d> wrote in
      news:t3k761dj2e kpm6gdhmjk1hq8o tc178k72h@4ax.c om:
      [color=blue]
      > On Mon, 18 Apr 2005 10:58:54 -0400, Martin < > wrote:[color=green]
      >> $data = "12.4,11.3,13.2 ";
      >> $myarray = explode(',',$da ta);
      >> I get strings, not numbers:.[/color]
      >
      > It's probably good enough; whenever you use variables in a numerical
      > context, they are treated as numbers, even if they are numbers in a
      > string.[/color]

      You know, I think this *used* to be true, but somewhere mid-PHP4 upgrades,
      a lot of my scripts (actually, ALL 'numbers' that were coming via explode
      ()) stopped working and I had to specifically float() the array contents.

      Comment

      • Martin

        #4
        Re: Reading commaseparated string into array

        On Mon, 18 Apr 2005 17:26:11 +0200, Ewoud Dronkert <firstname@last name.net.invali d> wrote:
        [color=blue]
        >On Mon, 18 Apr 2005 10:58:54 -0400, Martin < > wrote:[color=green]
        >> $data = "12.4,11.3,13.2 ";
        >> $myarray = explode(',',$da ta);
        >> I get strings, not numbers:.[/color]
        >
        >It's probably good enough; whenever you use variables in a numerical
        >context, they are treated as numbers, even if they are numbers in a
        >string. If you *have* to make them numbers first, try:[/color]

        Some of the classes I use will not work with arrays of strings, so thanks.[color=blue]
        >
        >function makefloat( $a )
        >{
        > return (float)$a;
        >}
        >$numdata = array_map( 'makefloat', $strdata ); // PHP >= 4.0.6[/color]

        Comment

        • Ewoud Dronkert

          #5
          Re: Reading commaseparated string into array

          On Mon, 18 Apr 2005 10:57:59 -0500, Good Man wrote:[color=blue][color=green]
          >> whenever you use variables in a numerical context, they are treated as
          >> numbers, even if they are numbers in a string.[/color]
          >
          > You know, I think this *used* to be true, but somewhere mid-PHP4 upgrades,
          > a lot of my scripts (actually, ALL 'numbers' that were coming via explode
          > ()) stopped working and I had to specifically float() the array contents.[/color]

          Really? This works fine in php 4.3.9 and php5:

          foreach ( explode( ',', '1,2.7,-3.14' ) as $n ) echo ($n + 1)."\n";

          gives

          2
          3.7
          -2.14


          --
          Firefox Web Browser - Rediscover the web - http://getffox.com/
          Thunderbird E-mail and Newsgroups - http://gettbird.com/

          Comment

          • Good Man

            #6
            Re: Reading commaseparated string into array

            Ewoud Dronkert <firstname@last name.net.invali d> wrote in
            news:crq7611bil 5h58d5r38sm2nlu on500s8sc@4ax.c om:

            [color=blue][color=green]
            >> You know, I think this *used* to be true, but somewhere mid-PHP4
            >> upgrades, a lot of my scripts (actually, ALL 'numbers' that were
            >> coming via explode ()) stopped working and I had to specifically
            >> float() the array contents.[/color]
            >
            > Really? This works fine in php 4.3.9 and php5:
            >
            > foreach ( explode( ',', '1,2.7,-3.14' ) as $n ) echo ($n + 1)."\n";
            >
            > gives
            >
            > 2
            > 3.7
            > -2.14[/color]

            interesting.... but yeah, really, scripts that were working fine for over a
            year just stopped working correctly (php upgrade) and i had to recode a few
            pages and float() each exploded number before multiplying against another
            variable (also a number).... i was confused as could be, i guess i still
            am!

            Comment

            Working...