Array and Search function

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

    Array and Search function

    Hello,

    I'm a new Php user.
    I'm trying to use the array functions and would like to ask a suggestion, if
    possible.
    I wrote the following sample code:

    <?php
    $test[1]="tizio";
    $test[2]="caio";
    $test[3]="caio";
    print(array_sea rch("caio", $test));
    ?>

    It's a simple array with a command which searches the corresponding key for
    the string "caio".
    As result I've 2 and not, as I thought 2, 3. Do you know is it possible to
    search all the keys associated to the string "caio"? I tried also the
    array_keys function but in this case the result has too much information.
    I'd like to have as result only 2 and 3.
    Pleace accept my apologies for the question and for the trouble.
    Really many thanks for the possible suggestions.

    Best,
    Gi


  • Laurent Duretz

    #2
    Re: Array and Search function

    Gi a écrit :
    Hello,
    >
    I'm a new Php user.
    I'm trying to use the array functions and would like to ask a suggestion, if
    possible.
    I wrote the following sample code:
    >
    <?php
    $test[1]="tizio";
    $test[2]="caio";
    $test[3]="caio";
    print(array_sea rch("caio", $test));
    ?>
    >
    It's a simple array with a command which searches the corresponding key for
    the string "caio".
    As result I've 2 and not, as I thought 2, 3. Do you know is it possible to
    search all the keys associated to the string "caio"? I tried also the
    array_keys function but in this case the result has too much information.
    I'd like to have as result only 2 and 3.
    Pleace accept my apologies for the question and for the trouble.
    Really many thanks for the possible suggestions.
    >
    Best,
    Gi
    >
    >
    Hi,

    You should use array_keys()

    Laurent Duretz

    Comment

    • Chung Leong

      #3
      Re: Array and Search function

      Gi wrote:
      Hello,
      >
      I'm a new Php user.
      I'm trying to use the array functions and would like to ask a suggestion, if
      possible.
      I wrote the following sample code:
      >
      <?php
      $test[1]="tizio";
      $test[2]="caio";
      $test[3]="caio";
      print(array_sea rch("caio", $test));
      ?>
      >
      It's a simple array with a command which searches the corresponding key for
      the string "caio".
      As result I've 2 and not, as I thought 2, 3. Do you know is it possible to
      search all the keys associated to the string "caio"? I tried also the
      array_keys function but in this case the result has too much information.
      I'd like to have as result only 2 and 3.
      Pleace accept my apologies for the question and for the trouble.
      Really many thanks for the possible suggestions.
      >
      Best,
      Gi
      The function you need is array_intersect (). Example:

      $test[1]="tizio";
      $test[2]="caio";
      $test[3]="caio";

      $search = array("caio");
      var_dump(array_ intersect($test , $search));

      Comment

      Working...