applying a function to each array element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawpoop@gmail.com

    applying a function to each array element

    Hello all -

    I would like to use the is_numeric() function to check each element of
    an array. I am just looking for a false if any element is not numeric,
    or a true if each element is numeric.

    Can I do this with a single function in a single like, or do I have to
    do a foreach{ ... break() } structure?
  • Jerry Stuckle

    #2
    Re: applying a function to each array element

    lawpoop@gmail.c om wrote:
    Hello all -
    >
    I would like to use the is_numeric() function to check each element of
    an array. I am just looking for a false if any element is not numeric,
    or a true if each element is numeric.
    >
    Can I do this with a single function in a single like, or do I have to
    do a foreach{ ... break() } structure?
    >
    You need to use a loop. is_numeric() will just return false on an
    array, because the array is not a numeric value.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Heiko Richler

      #3
      Re: applying a function to each array element

      lawpoop@gmail.c om wrote:
      I would like to use the is_numeric() function to check each element of
      an array. I am just looking for a false if any element is not numeric,
      or a true if each element is numeric.
      >
      Can I do this with a single function in a single like, or do I have to
      do a foreach{ ... break() } structure?

      You can do so. But it may be more difficult to understand:

      $result = (count(array_fi lter($arr, "is_numeric " ))=count($arr))



      or try this:

      $result = !in_array(false , array_map("is_n umeric", $arr))


      or try this:

      Once declare r_is_numeric like this:

      function r_is_numeric($l ogic, $element)
      {
      $logic = $logic and is_numeric($ele ment);
      return $logic;
      }

      All you need is:

      $result = array_reduce($a rr, "r_is_numer ic", true);




      Heiko

      Comment

      • onur.buyukceran@gmail.com

        #4
        Re: applying a function to each array element

        array_walk will do the job.

        Apply a user supplied function to every member of an array



        On May 14, 9:44 pm, lawp...@gmail.c om wrote:
        Hello all -
        >
        I would like to use the is_numeric() function to check each element of
        an array. I am just looking for a false if any element is not numeric,
        or a true if each element is numeric.
        >
        Can I do this with a single function in a single like, or do I have to
        do a foreach{ ... break() } structure?

        Comment

        • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

          #5
          Re: applying a function to each array element

          lawpoop@gmail.c om escribió:
          I would like to use the is_numeric() function to check each element of
          an array. I am just looking for a false if any element is not numeric,
          or a true if each element is numeric.
          >
          Can I do this with a single function in a single like, or do I have to
          do a foreach{ ... break() } structure?
          My two cents:

          function is_numeric_arra y(&$array){
          foreach($array as $i){
          if(!is_numeric( $ic)){
          return FALSE;
          }
          }
          return TRUE;
          }



          --
          -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
          -- Mi sitio sobre programación web: http://bits.demogracia.com
          -- Mi web de humor al baño María: http://www.demogracia.com
          --

          Comment

          Working...