0

I have the following code:

$recent = apc_fetch('recn');
$recent[9] = $recent[8];
$recent[8] = $recent[7];
$recent[7] = $recent[6];
$recent[6] = $recent[5];
$recent[5] = $recent[4];
$recent[4] = $recent[3];
$recent[3] = $recent[2];
$recent[2] = $recent[1];
$recent[1] = $recent[0];
$rec = array_pop($recent);
$recent[0] = $name;
apc_store('recn', $recent);

Each time the page is reloaded, I wanted $name to be 1st, and than move whatever is 2nd down the list, and so on. I echo the array like so:

echo "Most Recent Songs: <br>";
echo "1. " . $name. "<br>";
echo "2. " . $recent[1] . "<br>";
echo "3. " . $recent[2] . "<br>";
echo "4. " . $recent[3] . "<br>";
echo "5. " . $recent[4] . "<br>";
echo "6. " . $recent[5] . "<br>";
echo "7. " . $recent[6] . "<br>";
echo "8. " . $recent[7] . "<br>";
echo "9. " . $recent[8] . "<br>";
echo "10. " . $recent[9] . "<br>";

But only the first entry shows up.

5
  • 1
    Instead of manually moving every element down an index in the array, try using array_unshift. Commented May 26, 2014 at 2:29
  • @giaour how would I do that? Commented May 26, 2014 at 2:32
  • 1
    I think you're also misunderstanding array_pop. You're taking the last element off and storing that in APC. Isn't it $recent that you want to store in APC? Commented May 26, 2014 at 2:33
  • you should invest in a loop Commented May 26, 2014 at 2:34
  • yes @Chelsea, what do you mean RUjordan? Commented May 26, 2014 at 2:37

1 Answer 1

2

Found it! Thanks to @giaour for the array_unshift suggestion, and @Chelsea for helping me understand array_pop Heres my new (working) code:

$recent = apc_fetch('recn');
array_unshift($recent, $name);
$rec = array_pop($recent);
apc_store('recn', $recent);
Sign up to request clarification or add additional context in comments.

Comments

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.