which one is faster isset(), array_search(), in_array()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    which one is faster isset(), array_search(), in_array()

    Hi,

    i was wondering what was the best option to store a string in an array.
    let me explain: I only need one part of the elements in the array. i.e. i can use the key to store my string, or i can use the value to store the string.
    I'll make it clearer with some code:
    Code:
    $string = 'someString';
    $array = array();
    //first way to save
    $array[$string] = true;//array('someString' => true)
    //second way to save
    $array[] = $string; //array(0 => 'someString')
    so my question is: what is the most efficient option to save a string in an array if I only use one part of the elements (key or value) to store the strings.

    maybe how i will use that array helps:
    I will need to know afterwards, if the string is stored in the array.
    Code:
    //i get a string and i want to know if it is already in array
    in_array($string, $array);
    isset($array[$string]);
    ...
    wich one is the best?
    thanks!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Forget about which is fastest and follow your own logic: you're storing a string in an array, i.e. you're not using the string as an array index. Therefore, store the string in the array and then use in_array(). Makes sense, no?

    Comment

    • guillermobytes
      New Member
      • Jan 2010
      • 77

      #3
      ok it makes sense.
      thank you!

      Comment

      • philipwayne
        New Member
        • Mar 2010
        • 50

        #4
        isset is the fastest. But as markus said your not storing the index so it will not work correctly.

        Comment

        Working...