douao2019 2014-05-26 02:26
浏览 33
已采纳

PHP删除数组的第一个元素并将所有其他元素向上移动1

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.

  • 写回答

1条回答 默认 最新

  • doudaiyao0934 2014-05-26 02:46
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!