in_array

  • We can check if an element exists in a Ruby array using Array#include?.

    PHP’s in_array has a strict argument that will consider object type. Ruby does not consider a Fixnum of 1 and a String of '1' to be the same thing, and will always operate in the equivalent of PHP’s strict = true argument for this function.

    PHP

    $fruit = array('apple', 'banana', 'kiwi');
    $result = in_array('apple', $fruit, true);
    var_export($result);
    // => true

    Ruby

    fruit = ["apple", "banana", "kiwi"]
    p fruit.include?("apple")
    # => true

    Just like PHP, Ruby can check for an element’s existence in an array even if that element is an array (or any object).

    PHP

    $list = array(array('a', 'b'), 'c');
    $result = in_array(array('a', 'b'), $list);
    var_export($result);
    // => true

    Ruby

    list = [["a", "b"], "c"]
    p list.include?(["a", "b"])
    # => true

    see also

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.