problems changing point for coma...

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

    problems changing point for coma...

    I have an array with numbers from a excel file in 12345,78 format and y
    need to change the coma (,) for a point (.) to make this numbers float.
    I have this:

    str_replace("," ,".",$array_wit h_numbers); but does not work.

    Any help???

  • David Haynes

    #2
    Re: problems changing point for coma...

    Roco3D wrote:[color=blue]
    > I have an array with numbers from a excel file in 12345,78 format and y
    > need to change the coma (,) for a point (.) to make this numbers float.
    > I have this:
    >
    > str_replace("," ,".",$array_wit h_numbers); but does not work.
    >
    > Any help???
    >[/color]

    A brute force method:
    for( $i = 0; $i < count($array_wi th_numbers); $i++) {
    str_replace(',' , '.', $array_with_num bers[$i];
    }

    -david-

    Comment

    • Iván Sánchez Ortega

      #3
      Re: problems changing point for coma...

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      David Haynes wrote:
      [color=blue]
      > A brute force method:
      > for( $i = 0; $i < count($array_wi th_numbers); $i++) {
      > str_replace(',' , '.', $array_with_num bers[$i];
      > }[/color]

      Never use a for(;;) loop to iterate over an array: you are creating
      error-prone code.

      Either use foreach() with references:
      <?php
      foreach($array_ with_numbers as &number)
      str_replace(',' , '.', $number);
      ?>

      Or use array_map to automatically transverse the array applying a function
      to every element, or (even better) use a recursive function.

      Or, you can RTFM. http://php.net/str_replace , the part that says:

      "If subject is an array, then the search and replace is performed with every
      entry of subject, and the return value is an array as well."


      So, please go RTFM.

      - --
      - ----------------------------------
      Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

      "All truth passes through three stages. First, it is ridiculed. Second, it
      is violently opposed. Third, it is accepted as being self-evident."
      - Arthur Schopenhauer (1788-1860)
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.2 (GNU/Linux)

      iD8DBQFEDf863jc Q2mg3Pc8RAn5MAJ 9eJ+FYy7Zh6Dmr5 O1UfkIIGdnLTwCb BmKZ
      VD+NBSUgRGFj9HB bG206CSI=
      =VWqE
      -----END PGP SIGNATURE-----

      Comment

      • David Haynes

        #4
        Re: problems changing point for coma...

        Iván Sánchez Ortega wrote:[color=blue]
        > -----BEGIN PGP SIGNED MESSAGE-----
        > Hash: SHA1
        >
        > David Haynes wrote:
        >[color=green]
        >> A brute force method:
        >> for( $i = 0; $i < count($array_wi th_numbers); $i++) {
        >> str_replace(',' , '.', $array_with_num bers[$i];
        >> }[/color]
        >
        > Never use a for(;;) loop to iterate over an array: you are creating
        > error-prone code.
        >
        > Either use foreach() with references:
        > <?php
        > foreach($array_ with_numbers as &number)
        > str_replace(',' , '.', $number);
        > ?>
        >
        > Or use array_map to automatically transverse the array applying a function
        > to every element, or (even better) use a recursive function.
        >
        > Or, you can RTFM. http://php.net/str_replace , the part that says:
        >
        > "If subject is an array, then the search and replace is performed with every
        > entry of subject, and the return value is an array as well."
        >
        >
        > So, please go RTFM.[/color]

        I suspect you are having a bad day.
        Iteration by indexing works and is, as I said, brute force.

        -david-

        Comment

        • Toby Inkster

          #5
          Re: problems changing point for coma...

          David Haynes wrote:
          [color=blue]
          > Iteration by indexing works and is, as I said, brute force.[/color]

          Iteration by indexing works *iff* all your indexes are numeric and
          sequential. Your for-loop won't work on non-sequential arrays like this:

          array(
          0 => '123,45',
          1 => '123,45',
          9 => '123,45'
          );

          because count() will return 3, meaning that the last element of the array,
          with index 9, won't be touched. Nor will it work on:

          array(
          -1 => '123,45',
          0 => '123,45',
          1 => '123,45'
          );

          as your for-loop has a hard-coded start value of 0. Nor will it work on:

          array(
          'a' => '123,45',
          'b' => '123,45',
          'c' => '123,45'
          );

          because the keys aren't numeric. Iván's foreach-loop will work on all of
          these three examples.

          --
          Toby A Inkster BSc (Hons) ARCS
          Contact Me ~ http://tobyinkster.co.uk/contact

          Comment

          • David Haynes

            #6
            Re: problems changing point for coma...

            Toby Inkster wrote:[color=blue]
            > David Haynes wrote:
            >[color=green]
            >> Iteration by indexing works and is, as I said, brute force.[/color]
            >
            > Iteration by indexing works *iff* all your indexes are numeric and
            > sequential. Your for-loop won't work on non-sequential arrays like this:
            >
            > array(
            > 0 => '123,45',
            > 1 => '123,45',
            > 9 => '123,45'
            > );
            >
            > because count() will return 3, meaning that the last element of the array,
            > with index 9, won't be touched. Nor will it work on:
            >
            > array(
            > -1 => '123,45',
            > 0 => '123,45',
            > 1 => '123,45'
            > );
            >
            > as your for-loop has a hard-coded start value of 0. Nor will it work on:
            >
            > array(
            > 'a' => '123,45',
            > 'b' => '123,45',
            > 'c' => '123,45'
            > );
            >
            > because the keys aren't numeric. Iván's foreach-loop will work on all of
            > these three examples.
            >[/color]
            Tony,
            Thank you for a well presented response. I guess I tend to treat
            associative and numeric arrays differently (in my own head) and so would
            not even have thought of using a for() on an associative array, but the
            difficulties with non-zero origin and sparse data are real.

            Lesson learned.
            -david-

            Comment

            Working...