Actually, the question is vague in regard to what element 14 means, e.g.
$dataArray = range(1,20);
echo $dataArray[14]; // 15
returns the element with the numeric index 14. Since the array returned by range()
is zero-based, it is the 15th element. But when I remove the first element
unset($dataArray[0]);
echo $dataArray[14]; // 15
it is still returning 15 although it is now the really the 14th element. This is because an index does not denote position in the array. I could shuffle
the array and index 14 could end up at first position. Or the array could contain associative keys, e.g.
$dataArray = array('foo' => 'bar', 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
echo $dataArray[14]; // 15
and 14 would still return 15. If your $id
corresponds to the index number, the solutions given in the various answers so far solve your problem.
If you mean the position of the element in the array, you have to use an ArrayIterator
to seek
to the (zero-based) position:
$iterator = new ArrayIterator($dataArray);
$iterator->seek(13); // zero-based so 13 is the 14th element
echo $iterator->current(); // 13
or the regular array functions
reset($dataArray);
for($i=1; $i<14;$i++) next($dataArray);
echo current($dataArray); // 13
or somewhat shorter
echo current(array_slice($dataArray, 13, 0)); // 13
because
Pos Index Value
0: [foo] => bar
1: [0] => 1
2: [1] => 2
3: [2] => 3
4: [3] => 4
5: [4] => 5
6: [5] => 6
7: [6] => 7
8: [7] => 8
9: [8] => 9
10: [9] => 10
11: [10] => 11
12: [11] => 12
13: [12] => 13
14: [13] => 14
15: [14] => 15