title: How to access an element of a multidimensional array by a key that is in another linear array?
body: <p>We have a multidimensional associative array. It is necessary to address to an element of an array on a key which is written down in other linear array.</p>
  
  <p>That is, there is a multidimensional data array and a linear array with a key:</p>
  
  <pre><code>$a=[
    'animals'=&gt; [
      'cats' =&gt; [
        'catusdomesticus' =&gt; 'home',
        'pantera' =&gt; 'wild'
      ]
    ]
  ];
  
  $keys=['animals', 'cats', 'pantera'];
  </code></pre>
  
  <p>There are two things to do:</p>
  
  <ol>
  <li>(simple) Get the value of $a['animals']['cats']['pantera']</li>
  <li>(complex) Delete the element $a['animals']['cats']['pantera']</li>
  </ol>
  
  <p>How to do this in PHP5?</p>
  
  <p>Note: It is required to do without code generation and eval ().</p>
  
  <p><strong>UPD:</strong></p>
  
  <p>Get value is easy:</p>
  
  <pre><code>$item =&amp; $a;
  foreach($keys as $key)
    $item =&amp; $item[$key];
  var_dump( $item );
  </code></pre>
  
  <p>For remove element i try next code:</p>
  
  <pre><code>$item =&amp; $a;
  for($i=0; $i&lt;count($keys); $i++)
  {
    $item =&amp; $item[$keys[$i]];
  
    if($i===count($keys)-1) {
      echo "\nDelete element:\n";
      var_dump($item);
  
      unset($item);
    }
  }
  var_dump( $a );
  </code></pre>
  
  <p>But this is not work. The unset() by reference to array value cant remove array element.</p>
  
  <p><strong>UPD2:</strong></p>
  
  <p>Short solution for set value or remove element from array:</p>
  
  <pre><code>$item =&amp;$a;
  foreach($keys as $key) {
    $array =&amp; $item;
    $item =&amp; $item[$key];
  }
  $array[$key] = 42; // For set value
  OR
  unset($array[$key]); // For remove element
  </code></pre>