通过元素指针(别名)删除数组元素

发布于 2024-07-26 01:38:43 字数 618 浏览 4 评论 0原文

是否可以通过指针删除数组元素?

多维数组:

$list = array(
    1=>array(
          2=>array('entry 1'),
          3=>array('entry 2')
    ),
    4=>'entry 3',
    5=>array(
          6=>array('entry 4')
    )
);

参考数组:

$refs = array(
     1=>&$list[1], 
     2=>&$list[1][2],
     3=>&$list[1][3],
     4=>&$list[4],
     5=>&$list[5],
     6=>&$list[5][6]
);

参考数组仅包含指向多维数组元素的指针(别名)。 现在我想从 $list 数组中删除(取消链接)元素。

通过使用,

$n=3;
unset($refs[$n])

但PHP仅删除指针。

Is it possible to remove an array element by a pointer?

Multidimensional array:

$list = array(
    1=>array(
          2=>array('entry 1'),
          3=>array('entry 2')
    ),
    4=>'entry 3',
    5=>array(
          6=>array('entry 4')
    )
);

Reference array:

$refs = array(
     1=>&$list[1], 
     2=>&$list[1][2],
     3=>&$list[1][3],
     4=>&$list[4],
     5=>&$list[5],
     6=>&$list[5][6]
);

The reference array contains only pointer (alias) to the multidimensional array elements. Now I want to delete (unlink) the elements from the $list array.

By using,

$n=3;
unset($refs[$n])

But PHP only deletes the pointer.

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

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

发布评论

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

评论(2

盗梦空间 2024-08-02 01:38:43

您的引用数组似乎是错误的:

$refs = array(
     1 => &$list[1],
     2 => &$list[2],
     3 => &$list[3],
     4 => &$list[4],
     5 => &$list[5],
     6 => &$list[6]
);

但是您的 $list 数组不包含元素 236 。 因此 $refs 数组应该看起来像:

$refs = array(
     1 => &$list[1],
     2 => &$list[1][2],
     3 => &$list[1][3],
     4 => &$list[4],
     5 => &$list[5],
     6 => &$list[5][6]
);

根据您的要求,您可以执行以下操作:

$refs[2] = null;
unset($refs[2]);

但这会将 $list[1][2] 保留为数组元素包含NULL

编辑:

要从源数组$list中删除元素,您必须求助于一些递归搜索函数(未经测试 - 可能需要一些调整):

function removeElement($element, array &$array)
{
    foreach ($array as $key => &$value) {
        if ($element == $value) {
            unset($array[$key]);
            return true;
        } else if (is_array($value)) {
            $found = removeElement($element, $value);
            if ($found) {
                return true;
            }
        }
    }
    return false;
}

removeElement($refs[2], $list);

Your reference array seems to be wrong:

$refs = array(
     1 => &$list[1],
     2 => &$list[2],
     3 => &$list[3],
     4 => &$list[4],
     5 => &$list[5],
     6 => &$list[6]
);

But your $list array does not contain elements 2, 3 and 6. So the $refs array should rather look like:

$refs = array(
     1 => &$list[1],
     2 => &$list[1][2],
     3 => &$list[1][3],
     4 => &$list[4],
     5 => &$list[5],
     6 => &$list[5][6]
);

Depending on what your requirements are you could do:

$refs[2] = null;
unset($refs[2]);

But this will leave $list[1][2] as an array element containing NULL.

EDIT:

To remove the element from it's source array $list you have to resort to some recursive search function (untested - may need some tweaking):

function removeElement($element, array &$array)
{
    foreach ($array as $key => &$value) {
        if ($element == $value) {
            unset($array[$key]);
            return true;
        } else if (is_array($value)) {
            $found = removeElement($element, $value);
            if ($found) {
                return true;
            }
        }
    }
    return false;
}

removeElement($refs[2], $list);
小镇女孩 2024-08-02 01:38:43

是否可以存储对“父”元素和索引的引用,例如

<?php
$list = array(
  1=>array(
    2=>array('entry 1'),
    3=>array(
      4=>'entry 2',
      5=>'entry 3'
    )
  ),
  2=>'entry 3',
  5=>array(
    'foo'=>'entry 4',
    1=>'entry 99'
  )
);

// delete
$refs = array(
  // 'entry 2'
  array(&$list[1][3], 4),
  // and 'entry 4'
  array(&$list[5], 'foo')
);

foreach($refs as $r) {
    unset($r[0][$r[1]]);
}
print_r($list);

Would it be possible to store a reference to the "parent" element and the index, like

<?php
$list = array(
  1=>array(
    2=>array('entry 1'),
    3=>array(
      4=>'entry 2',
      5=>'entry 3'
    )
  ),
  2=>'entry 3',
  5=>array(
    'foo'=>'entry 4',
    1=>'entry 99'
  )
);

// delete
$refs = array(
  // 'entry 2'
  array(&$list[1][3], 4),
  // and 'entry 4'
  array(&$list[5], 'foo')
);

foreach($refs as $r) {
    unset($r[0][$r[1]]);
}
print_r($list);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文