简体   繁体   English

PHP如何在arrayObject上进行array_unshift

[英]PHP how to array_unshift on an arrayObject

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 ?如标题所述,如何在arrayObject上执行array_unshift()array_push()是通过执行arrayObject->append()获得的,但是 unshift 呢?

Edit: What I've forgot to mention is that i also need in this particular case to preserve existing keys.编辑:我忘记提及的是,在这种特殊情况下,我还需要保留现有密钥。

The API of ArrayObject does not have any function to accomplish this directly. ArrayObjectAPI没有任何直接完成此任务的功能。 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). 手动将每个元素移动1,并将新值设置为索引0(仅当您具有数字索引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). 编写一个派生自ArrayObject的类并添加一个函数prepend (实现可以是下面的那个)。
  • Extract an array, call array_unshift() and create a new ArrayObject with the modified array: 提取一个数组,调用array_unshift()并使用修改后的数组创建一个新的ArrayObject
 $array = $arrayObject->getArrayCopy();
 array_unshift($array, $new_value);
 $arrayObject->exchangeArray($array);

There is no such functionality in ArrayObject, but you can subclass it to add whatever you need. ArrayObject中没有这样的功能,但您可以将其子类化以添加您需要的任何内容。 Try this: 尝试这个:

class ExtendedArrayObject extends ArrayObject {
    public function prepend($value) {
        $array = (array)$this;
        array_unshift($array, $value);
        $this->exchangeArray($array);
    }
}
function prefix($value) {
    return $this->exchangeArray(array_merge([$value], $this->getArrayCopy()));
}

@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) @Dmitry,@ soulmerge你的答案很好,关于最初的问题,但在我的编辑中缺少了要求,但他们指出我正确的方向来实现我所期望的这里我们在这里工作的解决方案:(为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. 这些例子与我们特定的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). 我们使用数组值中的给定键作为值的键(考虑使用数据库rowId作为集合中每个值的索引)。 let's call this value "key" here's what the actual array struct looks like: 让我们在这里调用这个值“key”是实际的数组结构:

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. 谢谢你的答案, 如果你找到一种在php5.0中工作的方式,我仍然感兴趣。

this works for me.这对我有用。 with array_merge()array_merge()

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

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

    array_merge($new_arr,$old_arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM