0

I want to always keep three element in array. So, I want if new element is added in array then oldest element will remove from array and Re-index array. Which is the best and fast way to make it?

This is my first array

$foo = array(

    'when', // [0]
    'whom', // [1]
    'what' // [2]

);

If new element "how" added, I want to modify like below.

$foo = array(

    'how', // [0]
    'whom', // [1]
    'what' // [2]

);

4 Answers 4

2

PHP has many built in array functions including array_unshift which allows you to add an element to the beginning and array_pop which allows you to remove an element from the end:

array_unshift($foo, 'how'); // Add to the beginning
array_pop($foo); // Remove from the end

Example: https://eval.in/859692

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for trying to help... I want like this: (how > what > whom) but you code return (how > when > whom)...... i want to get data depend serial. Would you please help me....
I'm afraid I do not understand the logic. You said you want the "oldest" to be removed. How do you know that "what" is the oldest? I was under the impression if you are adding items to the beginning and removing from the end then the last item must be the "oldest" and should be removed?
I called oldest means first element which is "when".
Then why do "what" and "whom" change positions in the expected output array?
Thanks neuromatter for you hard trying to help me. But @Progrock will share a code which work perfectly...
0

PHP scripts are no persistent, so I would suggest using time() as a key for each value. Then you know which one was put in first, and just write a function to overwrite the oldest.

Comments

0
<?php
$queue        = [];
$queue_length = 3;

$add_item = function($item) use (&$queue, $queue_length) {
    array_unshift($queue, $item) &&
    count($queue) == $queue_length + 1 &&
    array_pop($queue);

    var_dump($queue);
};

$add_item('first');
$add_item('second');
$add_item('third');
$add_item('fourth');
$add_item('fifth');

Output:

array(1) {
  [0]=>
  string(5) "first"
}
array(2) {
  [0]=>
  string(6) "second"
  [1]=>
  string(5) "first"
}
array(3) {
  [0]=>
  string(5) "third"
  [1]=>
  string(6) "second"
  [2]=>
  string(5) "first"
}
array(3) {
  [0]=>
  string(6) "fourth"
  [1]=>
  string(5) "third"
  [2]=>
  string(6) "second"
}
array(3) {
  [0]=>
  string(5) "fifth"
  [1]=>
  string(6) "fourth"
  [2]=>
  string(5) "third"
}

Comments

0

The Fastest method You can Increase the More limit..

$array = ['One' => 1, 'Two' => 2, 'Three' => 3, 'Four' => 4, 'Five' => 5];
//selecting First 3 Arrays
foreach (new LimitIterator(new ArrayIterator($array), 0, 3) as $key => $val)
{
  echo "$key => $val\n";
}

Give a Test

7 Comments

I test your code... This output reverse. Would you tell me how can I get output without reverse... your output (two > three > four) but I want (four > three > two ).
Really? is that output two,three,four? i have tested that it shows One,two,three for me. :( it picks the 1st 3arrays... if add new array it will get the new and next 2 = 1st three arrays
I make one change.. First I tried your code.. It Output( One,two,three) but I user 1 Instead of 0 it output (two, three, four)
Could you Please give me the code what you have modified? let me test it
So you just need the last 3 array values? Use this to retrieve the last 3.. <?php $a=array("One","Two","Three","Four","Five"); //print output print_r(array_slice($a,-3)); ?>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.