i'm fairly new to php and I'm sure this is a simple solution. I have an array of numbers (i.e. 1,000,230 , 200,345,200 ) and I'd like to go through the array and remove the commas. What would be the easiest way to go about this? Thanks for the help in advance.
joeπŸ™‚

    Use a loop and use [man]str_replace/man to replace the commas with empty strings.

      Use implode("",$array);
      thats it..

      try this:

      <?php
      $arr = array(1,2,3,4,5);
      $new = implode("",$arr);
      echo $new;
      ?>

      regards,

      Dhaya

        kdhayanandan,

        You're method is not effective, as your array has no commas 'within the element values'.. your values are complete intergers that are separated by commas... case in point, change your first line to this instead and see if your solution removes commas:

        $arr = array('1,000,230', '200,345,200');
        

          I like the suggestion of using a hybrid of array_map and str_replace best:

          function stripCommas($x){
             $x = str_replace(',', '', $x);
             return $x;
          }
          
          $arr = array('1,000,230', '200,345,200', '100,789');
          $newArr = array_map("stripCommas", $arr);
          echo '<pre>'.print_r($newArr, true);
          

          Output:

          Array
          (
              [0] => 1000230
              [1] => 200345200
              [2] => 100789
          )
          

            It working now...

            <?php
            $arr = array('1,000,230', '200,345,200');
            foreach($arr as $key => $test){
            $arr[$key] = str_replace(",","",$test);
            }
            print_r($arr);
            ?>

            output:

            Array ( [0] => 1000230 [1] => 200345200 )

              nrg_alpha wrote:

              I like the suggestion of using a hybrid of array_map and str_replace best:

              Well, if you want to be fancy with functional programming, then why not go out all and use lambda functions? πŸ™‚

              $arr = array('1,000,230', '200,345,200', '100,789');
              $newArr = array_map(create_function('$x', 'return str_replace(",", "", $x);'), $arr);
              echo '<pre>'.print_r($newArr, true);

                @,

                Yes, that would work too.. that was basically what Laserlight's first post in this thread was hinting at...

                laserlight;10896396 wrote:

                Well, if you want to be fancy with functional programming, then why not go out all and use lambda functions? πŸ™‚

                $arr = array('1,000,230', '200,345,200', '100,789');
                $newArr = array_map(create_function('$x', 'return str_replace(",", "", $x);'), $arr);
                echo '<pre>'.print_r($newArr, true);

                TouchΓ©... I never make use of that anonymous 'create_function', but as you illustrated, it does come in handy πŸ™‚ While this latest solution does make 'code readability' a little harder, it is quite compact.. I feel this is almost equivalent to ternary operators from a standpoint of streamlining code.

                Very nice.

                  Write a Reply...