title: add or move key and value to first position in multidimensional array
body: <p>I have the following array, we'll call it <code>$arr</code> and I have prepared a sample array. I need to manipulate the path <code>$arr['svg']</code> to have a specific key and value always at index 0 position. This is a sample data-set and depending on the data I'm working with the key's and values are not fixed, however the main point is to always have the <strong>title array</strong> (key and value) at the top of the svg array.</p>
  <pre><code>$arr = array(&quot;svg&quot; =&gt; 
               array(
                   0 =&gt; array(&quot;@style&quot; =&gt; &quot;overflow:visible&quot;, &quot;@xlink:href&quot; =&gt; &quot;test.png&quot;),
                   1 =&gt; array(&quot;g&quot; =&gt; &quot;&quot;, &quot;@id&quot; =&gt; &quot;Layer_2&quot;),
                   2 =&gt; array(&quot;g&quot; =&gt; &quot;&quot;, &quot;@id&quot; =&gt; &quot;Layer_3&quot;),
                   3 =&gt; array(&quot;title&quot; =&gt; &quot;test&quot;)
               ),
               &quot;@version&quot; =&gt; 1.2,
               &quot;@baseProfile&quot; =&gt; &quot;tiny-ps&quot;,
               &quot;@id&quot; =&gt; &quot;Layer_1&quot;,
               &quot;@xmlns&quot; =&gt; &quot;http://www.w3.org/2000/svg&quot;
        );
  </code></pre>
  <p>I am trying to achieve two things under the array path $arr['svg']</p>
  <ol>
  <li>If the array key <strong>title</strong> exists in $arr['svg'] and it is not in index 0 position
  then move it to index 0 of $arr['svg'] and shift everything else
  down.</li>
  <li>If the array key <strong>title</strong> DOES NOT exist in $arr['svg'] then add it array('title' =&gt;
  'test') to index 0 position of $arr['svg'] and shift
  everything else down.</li>
  </ol>
  <p>The expected output of $arr will be like so:</p>
  <pre><code>Array
  (
      [svg] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [title] =&gt; test
                  )
              [1] =&gt; Array
                  (
                      [@style] =&gt; overflow:visible;
                      [@xlink:href] =&gt; test.png
                  )
  
              [2] =&gt; Array
                  (
                      [g] =&gt; 
                      [@id] =&gt; Layer_2
                  )
  
              [3] =&gt; Array
                  (
                      [g] =&gt; 
                      [@id] =&gt; Layer_3
                  )
  
          )
  
      [@version] =&gt; 1.2
      [@baseProfile] =&gt; tiny-ps
      [@id] =&gt; Layer_1
      [@xmlns] =&gt; http://www.w3.org/2000/svg
  )
  </code></pre>
  <p>I am trying to use this function to achieve this but it seems this function only works from the root array position $arr, not within a specific path $arr['svg']. If it can be modified to work within a specific path that would hopefully solve the issue.</p>
  <pre><code>//source: https://gist.github.com/wpscholar/0deadce1bbfa4adb4e4c
  function push_at_to_associative_array($array, $key, $new ){
      $keys  = array_keys( $array );
      $index = array_search( $key, $keys, true  );
      $pos   = false === $index ? count( $array ) : $index + 1;
          
      $array = array_slice($array, 0, $pos, true) + $new + array_slice($array, $pos, count($array) - 1, true);
      return $array;
  }
  
  Usage:
  
  $title = array(&quot;title&quot; =&gt; &quot;test');
  $arr = push_at_to_associative_array($arr, 'svg', $title);
  </code></pre>