How to append an array to another array index in PHP?
<p>I have the following problem, that first I would like to give an example of how I want to have the array as the final result:</p>

<pre><code>Array1Key1 = Array1Value1 
    =&gt; Array2Key1 = Array2Value1 
        =&gt; Array3Key1 = Array3Value1
    =&gt; Array2Key2 = Array2Value2 
        =&gt; Array3Key1 = Array3Value1
Array1Key2 = Array1Value2 
    =&gt; Array2Key1 = Array2Value1 
        =&gt; Array3Key1 = Array3Value1
</code></pre>

<p>So, I want to build an array that contains an array that contains another array, as I have showed in my example.</p>

<p>I'll Give the code that I have that creates the array:</p>

<pre><code>if(!is_array($this-&gt;ArrayExtentions))
{
    $arrayExtentions = array($fileInfo['type'] =&gt; $fileInfo['type'], array('Count' =&gt; 0));
    $this-&gt;ArrayExtentions[$fileInfo['extension']] = $arrayExtentions;
}
elseif(!isset($this-&gt;ArrayExtentions[$fileInfo['extension']]))
{
    $arrayExtentions = array($fileInfo['type'] =&gt; $fileInfo['type'], array('Count' =&gt; 0));
    $this-&gt;ArrayExtentions[$fileInfo['extension']] = $arrayExtentions;
}
elseif(!array_key_exists($fileInfo['type'], $this-&gt;ArrayExtentions[$fileInfo['extension']]))
{
    $arrayExtentions = array($fileInfo['type'] =&gt; $fileInfo['type'], array('Count' =&gt; 0));
    $this-&gt;ArrayExtentions[$fileInfo['type']] = $arrayExtentions;
}
</code></pre>

<p>In the last elseif, is for the case that I have the first key, but need to append a new array into it. </p>

<p><strong>Going from:</strong></p>

<pre><code>Array1Key1 = Array1Value1 
    =&gt; Array2Key1 = Array2Value1 
        =&gt; Array3Key1 = Array3Value1
</code></pre>

<p><strong>To</strong></p>

<pre><code>Array1Key1 = Array1Value1 
    =&gt; Array2Key1 = Array2Value1 
        =&gt; Array3Key1 = Array3Value1
    =&gt; Array2Key2 = Array2Value2 
        =&gt; Array3Key1 = Array3Value1
</code></pre>

<p>As you can see, I want to access the Array with a customized index. The problem is that, when i try that case i have showed in the last example, it just don't work. So, the last else is overwriting the array in the position i am specifying instead of putting a new one there.</p>

<p>Can someone help with my solution or come with a new one ?</p>

<p><strong>EDIT:</strong></p>

<p>I have found another solution, that is much simple and that actually works.
First of all I build a function the can search into the Multidimensional Array, and return its inner Array, and the Index (Key) by reference if it's needed.</p>

<pre><code>public function ArrayExtensionSearch($MultidimensionalArray, $Extension, $Type, &amp;$Key=0)
{
    if(is_array($MultidimensionalArray))
    {
        foreach($MultidimensionalArray as $Key=&gt;$array)
        {
            if($array['Extension'] == $Extension &amp;&amp; $array['Type'] == $Type)
                    return $array;
        }
    }
    return NULL;
}
</code></pre>

<p>And above is how I am using it, not only adding new elements when needed but i am also increasing the value of an attribute of the inner array. The other solution would have 3 Arrays(Array of array of array), with unique keys, but that is not really needed, i just needed an array of array, and the inner array is the one to have the unique keys, anything difference from the logic i have established will just generate a new entry in the array. Here is the code:</p>

<pre><code>$array = $this-&gt;ArrayExtensionSearch($this-&gt;ArrayExtentions, $fileInfo['extension'], $fileInfo['type'], $keyArray);
if($array == NULL)
{
    $array = array("Count" =&gt; 1, "Extension" =&gt; $fileInfo['extension'], "Type" =&gt; $fileInfo['type']);
    array_push($this-&gt;ArrayExtentions, $array);
}
else $this-&gt;ArrayExtentions[$keyArray]['Count']++;
</code></pre>