String mutation in array elements
<p>I would like to manipulate array elements. So if a certain array element ends with the letter <code>n</code> or <code>m</code> and the following element is for example <code>apple</code> then I want to delete the "a" of "apple" so that I get as an output: <code>Array ( [0] =&gt; man [1] =&gt; pple )</code></p>

<p>My Code:</p>

<pre><code>$input = array("man","apple");

$ending = array("m","n");

$example = array("apple","orange");


for($i=0;$i&lt;count($input);$i++)
{
   $second = isset( $input[$i+1])?$input[$i+1][0]:null;


   $third = substr($input[$i],-2);




        if( isset($third) &amp;&amp; isset($second) ){
                    if (   in_array($third,$ending) &amp;&amp; in_array($second,$example) ){
                    $input[$i+1] = substr($input[$i+1],0,-2);
                    }

        }   



}  
</code></pre>

<p>How do I have to change my code so that I get the desired output?</p>