I have a litte program that seems to reliably use push, pop and shift in PHP on an associative array. It works on the elements in the order. But… is it reliable that is has to work this way? I need an associative array that can reliably add elements after the last one and can reliably remove the oldest element.
<?php
$a = [];
$a['first'] = '1';
$a['second'] = '2';
$a[8] = '3';
$a['forth'] = '4';
print_r($a);
print "-- now push two values --n";
$a[] = '1st pushed value';
$a[] = '2nd pushed value';
print_r($a);
print "-- now pop last --n";
array_pop($a);
print_r($a);
print "-- now shift --n";
array_shift($a);
print_r($a);
print "-- now pop last --n";
array_pop($a);
print_r($a);
?>
To clarify: yes, I’ve read the documentation, but what is the first element of an array [‘test’ => ‘t’, 0 => ‘e’]? Is it ‘test’ or is it 0? Can I rely that ‘first’ is the first element put into an array when it comes to those functions? reliable means that it is no quirk but so intended. And no, I find the documentation very vague on that.