PHP中的队列数组[关闭]

I want to implement a queue in PHP, and looking at the manual , I found this example:

$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);

This creates the array:

array('apple', 'raspberry', 'orange', 'banana');

In this case 'banana' is at the beginning of the queue and it can be retrieved using array_pop().

I guess that might be the traditional approach, but is there any good reason for not reversing the data in the array as follows?

$queue = array('apple', 'orange');
$queue[] = 'banana';//avoid function call
array_push($queue, 'strawberry', 'grape');//add multiple items
$next = array_shift($queue);

Maybe it's trivial, but in that way you could avoid a function call when adding a single element. Is there some other good reason for not doing it that way?

EDIT:

It appears that my question was a little hard to understand, so to make it easier to see that my method really does implement a queue according to the FIFO principle, I wrote this code to correspond with the example from the PHP manual, producing the exact same array (except in reverse order):

$queue = array('banana', 'orange');
$queue[] = 'rasberry';
$queue[] = 'apple';

This creates the array:

array('banana', 'orange', 'rasberry', 'apple');

It's the exact same data but in reverse order, so to retreive the next item you would do so with:

$next = array_shift($queue);//The value of $next is 'banana' as before.

As already pointed out by the answers, this runs up against how most people visualize a queue. It seems that readability is the major issue. However, I find it easier to code. To me, it actually seems more natural, because the square bracket notation [] is the doorway through which my array elements enter in numerous circumstances. Therefore, implementing either a stack or a queue really isn't a question about how I mentally visualize my data. It's a question of what function to use to access the first or last element that passed through the door. For a queue it's array_shift(), and for a stack it's pop().

I would do it the first way (shortest code, easiest to follow) unless you have a specific and valid reason for using the second approach. While $queue[] = 'banana' may be faster (I believe it is but don't know for sure), the difference is so small that you shouldn't worry about it unless you're doing millions of operations or something where it would actually make a difference.

There's a difference before first approach and second - apart function calling.

array_unshift() will add your elements "at the top" (read them as in first position) of your array. $queue[] will ad element at the bottom.

Except this, both approach are equal valid

Edit

"There any good reason for not reversing the data?"

Yes, there is: if you want to implement a "classic" queue (read it as FIFO) you have to use first approach; is faster, is more readable and will not introduce "semantical" errors. With second method, as you insert a single element on the bottom of the queue, you're not implementing a FIFO.

$queue = array("apple", "orange");
array_unshift($queue, "banana");
array_unshift($queue, 'strawberry', 'grape');

echo "First Approach<br/>";
print_r($queue);

$queue = array('apple', 'orange');
$queue[] = 'banana';//avoid function call
array_push($queue, 'strawberry', 'grape');//add multiple items
$next = array_shift($queue);

echo "Second Approach<br/>";
print_r($queue);

will produce a different output

First Approach
Array ( [0] => strawberry [1] => grape [2] => banana [3] => apple [4] => orange ) Second Approach
Array ( [0] => orange [1] => banana [2] => strawberry [3] => grape ) 

answer is here in front of you