Can't find built-in PHP function which I know exists...

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

    Can't find built-in PHP function which I know exists...

    Hi Everyone. I know this is a stupid question, but I have been sifting
    through php.net and I am about ready to pull my hair out over it...

    I have seen a function similar to in_array() and array_search() which
    returns the 0-based index number of the specified element if it exists.

    Please could someone point me in the right direction?

    Many thanks!

    Daz.

  • peter

    #2
    Re: Can't find built-in PHP function which I know exists...

    Hi Everyone. I know this is a stupid question, but I have been sifting
    through php.net and I am about ready to pull my hair out over it...
    >
    I have seen a function similar to in_array() and array_search() which
    returns the 0-based index number of the specified element if it exists.
    >
    Please could someone point me in the right direction?
    do you mean reset($array)?


    Comment

    • Pedro Graca

      #3
      Re: Can't find built-in PHP function which I know exists...

      Daz wrote:
      I have seen a function similar to in_array() and array_search() which
      returns the 0-based index number of the specified element if it exists.
      >
      Please could someone point me in the right direction?
      I don't think there's such a function built-in into PHP;
      but you can build your own ...

      <?php
      function array_function( $needle, $array) {
      $n = 0;
      foreach ($array as $value) {
      if ($needle == $value) return $n;
      ++$n;
      }
      }

      $data = array('name'=>' forty-two', 'dob'=>'2006-10-21', 'eye-color'=>'brown' );
      $zero_index = (int)array_func tion('brown', $data);

      ### if ($zero_index != 2) {
      ### // error!
      ### }
      ?>

      What you would use it for is beyond my imagination :)

      --
      File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

      Comment

      • Pedro Graca

        #4
        Re: Can't find built-in PHP function which I know exists...

        Pedro Graca wrote:
        <?php
        function array_function( $needle, $array) {
        $n = 0;
        foreach ($array as $value) {
        if ($needle == $value) return $n;
        ++$n;
        }
        return false;
        }
        >
        $data = array('name'=>' forty-two', 'dob'=>'2006-10-21', 'eye-color'=>'brown' );
        $zero_index = (int)array_func tion('brown', $data);
        >
        ### if ($zero_index != 2) {
        ### // error!
        ### }
        ?>
        --
        File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

        I don't check the dodgeit address (very often).
        If you *really* need to mail me,
        use the address in the Reply-To header.

        Comment

        • Andy Hassall

          #5
          Re: Can't find built-in PHP function which I know exists...

          On 21 Oct 2006 11:40:15 -0700, "Daz" <cutenfuzzy@gma il.comwrote:
          >Hi Everyone. I know this is a stupid question, but I have been sifting
          >through php.net and I am about ready to pull my hair out over it...
          >
          >I have seen a function similar to in_array() and array_search() which
          >returns the 0-based index number of the specified element if it exists.
          Isn't that what array_search already does on numerically-indexed arrays?

          Or do you want the index of the key for the element, in the array of keys for
          the array? In which case it sounds like two array_search() calls:

          <?php
          $a = array(
          'fish' ='fingers',
          'dog' ='hot dog',
          'cow' ='steak',
          );

          print array_search(ar ray_search('hot dog', $a), array_keys($a)) ;
          ?>

          Output: 1

          --
          Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
          http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

          Comment

          • Daz

            #6
            Re: Can't find built-in PHP function which I know exists...


            Pedro Graca wrote:
            Pedro Graca wrote:
            <?php
            function array_function( $needle, $array) {
            $n = 0;
            foreach ($array as $value) {
            if ($needle == $value) return $n;
            ++$n;
            }
            >
            return false;
            >
            }

            $data = array('name'=>' forty-two', 'dob'=>'2006-10-21', 'eye-color'=>'brown' );
            $zero_index = (int)array_func tion('brown', $data);

            ### if ($zero_index != 2) {
            ### // error!
            ### }
            ?>
            Ok, I am happy yo build my own, I could have just swore that I saw a
            function that deoes it.

            Thanks for your input.

            Comment

            • Daz

              #7
              Re: Can't find built-in PHP function which I know exists...


              Andy Hassall wrote:
              On 21 Oct 2006 11:40:15 -0700, "Daz" <cutenfuzzy@gma il.comwrote:
              >
              Hi Everyone. I know this is a stupid question, but I have been sifting
              through php.net and I am about ready to pull my hair out over it...

              I have seen a function similar to in_array() and array_search() which
              returns the 0-based index number of the specified element if it exists.
              >
              Isn't that what array_search already does on numerically-indexed arrays?
              >
              Or do you want the index of the key for the element, in the array of keys for
              the array? In which case it sounds like two array_search() calls:
              >
              <?php
              $a = array(
              'fish' ='fingers',
              'dog' ='hot dog',
              'cow' ='steak',
              );
              >
              print array_search(ar ray_search('hot dog', $a), array_keys($a)) ;
              ?>
              >
              Output: 1
              >
              Hi Andy. Now that achieves exactly what I am looking for without
              iterating through the whole array as such.

              Many thanks.

              Daz

              Comment

              Working...