title: Get Element at N position in an Array without Loop
body: <p>How get you get element <code>key</code> and <code>value</code> of an at the <code>n</code> position array at a particular position without loop.</p>
  
  <p>Imagine</p>
  
  <pre><code>$postion = 3; // get array at 3rd position
  $array = array(
          "A" =&gt; "Four",
          "B" =&gt; "twp",
          "C" =&gt; "three",
          "D" =&gt; "Four",
          "E" =&gt; "Five",
          "F" =&gt; "Four");
  
  
  $keys = array_keys($array);
  $value = array_values($array);
  
  echo implode(array_slice($keys, $postion, 1)), PHP_EOL; // Key at 3rd posstion
  echo implode(array_slice($value, $postion, 1)), PHP_EOL; // Value at n position
  </code></pre>
  
  <p>Output </p>
  
  <pre><code>D
  Four
  </code></pre>
  
  <p>Issues With the method is </p>
  
  <ul>
  <li>Multiple Duplication of the array resulting higher memory usage</li>
  </ul>
  
  <p>Why not use loop</p>
  
  <ul>
  <li>You have to get multiple position multiple times .. looping large data set not efficient either </li>
  </ul>
  
  <p>Why not use a Database</p>
  
  <ul>
  <li>Yes working with memory based database like Redis can  make life easier but am particular array optimisation</li>
  </ul>
  
  <p>Why not use <code>SplFixedArray</code></p>
  
  <ul>
  <li><p>This would have been  solution but i the follow weer because am not using positive keys ( I really this is nor fair on php part)</p>
  
  <pre><code>Fatal error: Uncaught exception 'InvalidArgumentException' 
  with message 'array must contain only positive integer keys' 
  </code></pre></li>
  </ul>
  
  <p>What do you mean by large data set :</p>
  
  <ul>
  <li>Actually i stumble on this issue when trying to as this question <a href="https://stackoverflow.com/questions/14880830/managing-mega-arrays-in-php">Managing mega Arrays in PHP</a> so am looking at <code>1e6</code> or <code>1e7</code> with <code>512M memory limit</code></li>
  </ul>
  
  <p>Am sure something like <code>fseek</code> for array would do the trick .. but not sure if that exists </p>