# InArray

`Zend_Validate_InArray` allows you to validate if a given value is contained within an array. It is also able to validate multidimensional arrays.

## Simple array validation

The simplest way, is just to give the array which should be searched against at initiation:

    $validator = new Zend_Validate_InArray(array('key' => 'value',
                                                 'otherkey' => 'othervalue'));
    if ($validator->isValid('value')) {
        // value found
    } else {
        // no value found
    }

This will behave exactly like PHP's `in_array()` method.

Per default this validation is not strict nor can it validate multidimensional arrays.

Of course you can give the array to validate against also afterwards by using the `setHaystack()` method.

    $validator = new Zend_Validate_InArray();
    $validator->setHaystack(array('key' => 'value', 'otherkey' => 'othervalue'));

    if ($validator->isValid('value')) {
        // value found
    } else {
        // no value found
    }

## Strict array validation

As mentioned before you can also do a strict validation within the array. Per default there would be no difference between the integer value 0 and the string "0". When doing a strict validation this difference will also be validated and only same types are accepted.

A strict validation can also be done by using two different ways. At initiation and by using a method. At initiation you have to give an array with the following structure:

    $validator = new Zend_Validate_InArray(
        array(
            'haystack' => array('key' => 'value', 'otherkey' => 'othervalue'),
            'strict'   => true
        )
    );

    if ($validator->isValid('value')) {
        // value found
    } else {
        // no value found
    }

The haystack key contains your array to validate against. And by setting the strict key to `TRUE`, the validation is done by using a strict type check.

Of course you can also use the `setStrict()` method to change this setting afterwards.

Note that the strict setting is per default `FALSE`.

## Recursive array validation

In addition to PHP's `in_array()` method this validator can also be used to validate multidimensional arrays.

To validate multidimensional arrays you have to set the recursive option.

    $validator = new Zend_Validate_InArray(
        array(
            'haystack' => array(
                'firstDimension' => array('key' => 'value',
                                          'otherkey' => 'othervalue'),
                'secondDimension' => array('some' => 'real',
                                           'different' => 'key')),
            'recursive' => true
        )
    );

    if ($validator->isValid('value')) {
        // value found
    } else {
        // no value found
    }

Your array will then be validated recursive to see if the given value is contained.

Note that per default the recursive validation is turned off.