7

As stated in the title, How do you perform an array_unshift() on a arrayObject, array_push() is obtained by doing an arrayObject->append() but what about unshift ?

Edit: What I've forgot to mention is that i also need in this particular case to preserve existing keys.

malko
  • 2,292
  • 18
  • 26

5 Answers5

10

The API of ArrayObject does not have any function to accomplish this directly. You have several other options:

  • Manually move each element by 1, and set your new value at index 0 (only if you have a numerically index ArrayObject).
 $tmp = NULL;
 for ($i = 0; $arrayObject->offsetExists($i + 1); $i++) {
      $tmp = $arrayObject->offsetGet($i + 1);
      $arrayObject->offsetSet($i + 1, $arrayObject->offsetGet($i));
 }
 $arrayObject->offsetSet($i, $tmp);
 $arrayObject->offsetSet(0, $new_value);
  • Write a class deriving from ArrayObject and add a function prepend (implementation could be the one below).
  • Extract an array, call array_unshift() and create a new ArrayObject with the modified array:
 $array = $arrayObject->getArrayCopy();
 array_unshift($array, $new_value);
 $arrayObject->exchangeArray($array);
phant0m
  • 16,595
  • 5
  • 50
  • 82
soulmerge
  • 73,842
  • 19
  • 118
  • 155
6

There is no such functionality in ArrayObject, but you can subclass it to add whatever you need. Try this:

class ExtendedArrayObject extends ArrayObject {
    public function prepend($value) {
        $array = (array)$this;
        array_unshift($array, $value);
        $this->exchangeArray($array);
    }
}
lazyhammer
  • 1,228
  • 1
  • 11
  • 14
2
function prefix($value) {
    return $this->exchangeArray(array_merge([$value], $this->getArrayCopy()));
}
Quolonel Questions
  • 6,603
  • 2
  • 32
  • 33
1

@Dmitry, @soulmerge your answers was good regarding the initial question but was missing the requirement in my edit, but they point me in the right direction to achieve what i was expected here's the solution we came with here at work: (work for php>=5.1)

public function unshift($value){
  $tmp = $this->getArrayCopy();
  $tmp = array($value) + $tmp;
  $this->exchangeArray($tmp);
  return $this;
}

these exemple is not exactly the same as the final solution we needed as for our particular arrayObject. We use a given key in array values as key for the values (think of using database rowId as index for each value in the collection). let's call this value "key" here's what the actual array struct looks like:

array(
  key1 => array(key=>key1,val1=>val1,val2=>val2...),
  key2 => array(key=>key2,val1=>val1_2,val2=>val2_2...),
  ...
);

so our solution looks more something like this:

public function unshift($value){
  $tmp = $this->getArrayCopy();
  $tmp = array($value['key'],$value) + $tmp;
  $this->exchangeArray($tmp);
  return $this;
}

thank's for your answers, if you find a way that work in php5.0 too i'm still interested.

malko
  • 2,292
  • 18
  • 26
  • 1
    really? php5.0??? It's been out of support for *eight years*. Aside from missing out on all the new features, it also has numerous known bugs and some nasty security problems that are not going to be patched. If you're still using 5.0, it means your server admin is being dangerously negligent. (the same applies for 5.1, and I would even say for 5.2) – Spudley Jun 16 '13 at 20:20
0

this works for me. with array_merge()

    $old_arr[]=array(
        'val1'=>'sample1',
        'val2'=>'1'
    );

    $new_arr[] = array(
        'val1'=>'sample2',
        'val2'=>'2'
    );

    array_merge($new_arr,$old_arr);
Zap
  • 1
  • 1