PHP 如何对 arrayObject 进行 array_unshift

发布于 2024-11-27 06:24:31 字数 206 浏览 1 评论 0原文

正如标题中所述,如何在 arrayObject 上执行 array_unshift()array_push() 是通过执行 获得的arrayObject->append() 但 unshift 又如何呢?

编辑:我忘记提到的是,在这种特殊情况下我还需要保留现有的密钥。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

俏︾媚 2024-12-04 06:24:31

ArrayObject 的 API 没有任何函数可以直接完成此操作。您还有其他几个选项:

  • 手动将每个元素移动 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);
  • 编写一个从 ArrayObject 派生的类,并添加一个 prepend 函数(实现可以是下面的)。
  • 提取一个数组,调用 array_unshift() 并使用修改后的数组创建一个新的 ArrayObject
 $array = $arrayObject->getArrayCopy();
 array_unshift($array, $new_value);
 $arrayObject->exchangeArray($array);

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);
守不住的情 2024-12-04 06:24:31

ArrayObject 中没有这样的功能,但您可以对其进行子类化以添加您需要的任何内容。试试这个:

class ExtendedArrayObject extends ArrayObject {
    public function prepend($value) {
        $array = (array)$this;
        array_unshift($array, $value);
        $this->exchangeArray($array);
    }
}

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);
    }
}
谁许谁一生繁华 2024-12-04 06:24:31
function prefix($value) {
    return $this->exchangeArray(array_merge([$value], $this->getArrayCopy()));
}
function prefix($value) {
    return $this->exchangeArray(array_merge([$value], $this->getArrayCopy()));
}
清欢 2024-12-04 06:24:31

@Dmitry,@soulmerge,关于最初的问题,您的答案很好,但缺少我编辑中的要求,但它们为我指明了正确的方向,以实现我的预期,这是我们在工作中提供的解决方案:(适用于 php> ;=5.1)

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

这些示例与我们特定 arrayObject 所需的最终解决方案并不完全相同。我们使用数组值中的给定键作为值的键(考虑使用数据库 rowId 作为集合中每个值的索引)。让我们称这个值为“key”,这就是实际的数组结构的样子:

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

所以我们的解决方案看起来更像这样:

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

谢谢你的回答,如果你找到一种在 php5.0 中也能工作的方法,我仍然感兴趣.

@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.

岁月如刀 2024-12-04 06:24:31

这对我有用。与array_merge()

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

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

    array_merge($new_arr,$old_arr);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文