Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1045431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:59:02+00:00 2026-05-16T15:59:02+00:00

Consider a simple PHP ArrayObject with two items. $ao = new ArrayObject(); $ao[] =

  • 0

Consider a simple PHP ArrayObject with two items.

$ao = new ArrayObject();
$ao[] = 'a1';  // [0] => a1
$ao[] = 'a2';  // [1] => a2

Then delete the last item and add a new item.

$ao->offsetUnset(1);
$ao[] = 'a3';  // [2] => a3

I’d very much like to be able to have ‘a3’ be [1].

How can I reset the internal pointer value before I add ‘a3’?

I have a simple function that does this but I’d rather not copy the array if I don’t have to.

function array_collapse($array) {
    $return = array();
    while ($a = current($array)) {
        $return[] = $a;
        next($array);
    }
    return $return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:59:02+00:00Added an answer on May 16, 2026 at 3:59 pm

    With the expansion on the question in your comments: you’d have to extend the ArrayObject class to get this kind of behavior:

    class ReindexingArray extends ArrayObject {
        function offsetUnset($offset){
            parent::offsetUnset($offset);
            $this->exchangeArray(array_values($this->getArrayCopy()));
        }
        //repeat for every other function altering the values.
    }
    

    Another option would be the SplDoublyLinkedList:

    <?php
    $u = new SplDoublyLinkedList();
    $array = array('1' => 'one',
                   '2' => 'two',
                   '3' => 'three');
    foreach($array as $value) $u[] = $value;
    var_dump($u);
    unset($u[1]);        
    var_dump($u);
    $u[] = 'another thing';
    var_dump($u);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this simple PHP page: <?php $db = new mysqli(localhost, myuser, mypwd, mydb); if
Consider a simple JS event of document.getElementsByClassName('test')[0].onclick=function(){ document.getElementsByClassName('test')[0].innerHTML = 'New Text'; } How can
Consider simple XML document: <html><body> <table> <tr><td> Item 1</td></tr> <tr><td> Item 2</td></tr> </table> </body></html>
Consider this simple example; <?php $text = test; ?> <script type=text/javascript defer=defer> var test;
I would like to ask probably simple question. Consider following php page: <p>Name of
This seems like it should be simple. Any pointers would be much appreciated. Consider
Consider a simple Enumerator like this: natural_numbers = Enumerator.new do |yielder| number = 1
Consider simple multiple select box <select name=waste id=waste multiple> <option value=5>garbage</option> <option value=6>food</option> <option
Consider this simple model: A base Location table: +-------------------------------+ | Locations | +-------------------------------+ |(PK)
Consider a simple JS function as function test(property, color) { var element = document.getElementById(test);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.