Handling missing array offsets
<p>Consider the following piece of code:</p>

<pre><code>$tests = array( 
array ("a", "b", "c"),  array ("1", "2", "3"), array ("!", "@")
);

foreach ($tests as $test) 
 test($test[0], $test[1], $test[2]);

function test($param1, $param2, $param3) {
 // do whatever
}
</code></pre>

<p>This will work with no issues until it gets to the $test[2], which of course doesn't have a third element in the array, causing PHP to spit out:</p>

<pre><code>Notice: Undefined offset: 2
</code></pre>

<p>Is there a way to get around this besides:</p>

<pre><code>foreach ($tests as $test) {
 if (count($x) == 2) 
  test($test[0], $test[1]);
 else 
  test($test[0], $test[1], $test[2]);
}

function test($param1, $param2, $param3=null) {
 // do whatever
}
</code></pre>

<p>Which gets unwieldy as the size of each $test array gets bigger.
Or should I just ignore the notice after all?</p>

<p><strong>EDIT:</strong> Here is what I am actually trying to do:</p>

<pre><code>// wanted this:
function validate() {
    $pass = true;
    $rules = array (array ('field1', '!=', 'banana'),
            array('field2', 'notempty')
    );

    for ($i=0; $i&lt;count($rules) &amp;&amp; $pass; $i++)
        $pass = check($rules[$i][0], $rules[$i][1], $rules[$i][1]);

    return $pass;
}

function check($field, $operator, $expected) {
    $value = $this-&gt;getValue($field);

    switch ($operator) {
        case '!=':
            $pass = ($value != $expected);
            break;

        case '==':
            $pass = ($value == $expected);
            break;

        case 'empty':
            $pass = empty($value);
            break;

        default:
            $pass = !empty($value);
            break;
    }

    return $pass;
}

//instead of
function validate() {
    $pass = true;

    for ($i=0; $i&lt;count($rules) &amp;&amp; $pass; $i++)
        $pass = check($rules[$i]);

    return $pass;
}

function check($check) {
    $value = $this-&gt;getValue($check[0]);

    switch ($check[1]) {
        case '!=':
            $pass = ($value != $check[2]);
            break;

        case '==':
            $pass = ($value == $check[2]);
            break;

        case 'empty':
            $pass = empty($value);
            break;

        default:
            $pass = !empty($value);
            break;
    }

    return $pass;
}
</code></pre>

<p>Basically for stylistic reasons.</p>
