取消设置 php 引用

发布于 2024-08-17 01:49:52 字数 92 浏览 4 评论 0原文

所以我有这个函数,它返回对传入数组的特定点的引用。我想调用 unset ,然后从数组/引用中删除结果,但调用 unset 仅删除引用,不是原始数组中的数据。有什么想法吗?

So I have this function, and it returns me a reference to a particular point to the array passed in. I want to make a call to unset that will then remove the result from the array/reference, but calling unset only removes the reference, not the data from the original array. Any thoughts?

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

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

发布评论

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

评论(3

忘羡 2024-08-24 01:49:52

将引用设置为 null 将破坏该引用(以及任何其他引用)链接到的数据。

有关详细信息,请参阅手册中的取消设置引用。基本上你想要执行以下操作(摘自评论):

$a = 1;
$b =& $a;
$c =& $b;  //$a, $b, $c reference the same content '1'

$b = null; //All variables $a, $b or $c are unset

在你的情况下,它看起来像这样:

$a =& getArrayReference($whatever);
$a = null;

编辑

为了消除任何误解,以下是取消设置数组引用时得到的结果:

$arr = array('x','y','z');

$x =& $arr[1];
unset($x);
print_r($arr);
//gives Array ( [0] => x [1] => y [2] => z )

$x =& $arr[1];
$x = null;
print_r($arr);
//gives Array ( [0] => x [1] => [2] => z ) 

请注意,在第一个示例中,第二个数组索引并未使用 unset() 删除其内容,但第二个示例将引用设置为 null 来完成此操作。

注意:如果您还需要取消设置数组索引(我不太清楚您是否这样做),那么您需要找到一种方法来引用数组的键而不是值,可能是通过改变函数的返回值来实现的。

Setting the reference to null will destroy the data that the reference (and any other reference) is linked to.

See Unsetting References in the manual for more on this. Basically you want to do the following (taken from the comments):

$a = 1;
$b =& $a;
$c =& $b;  //$a, $b, $c reference the same content '1'

$b = null; //All variables $a, $b or $c are unset

In your case, it'll look something like this:

$a =& getArrayReference($whatever);
$a = null;

EDIT

To clear up any misconceptions, here's what you get when you unset array references:

$arr = array('x','y','z');

$x =& $arr[1];
unset($x);
print_r($arr);
//gives Array ( [0] => x [1] => y [2] => z )

$x =& $arr[1];
$x = null;
print_r($arr);
//gives Array ( [0] => x [1] => [2] => z ) 

Notice how the second array index does not have it's content deleted in the first example with unset(), but the second example of setting the reference to null accomplishes this.

Note: If you need to unset the array index as well, which I'm a bit unclear on as to whether you do or not, then you'll need to find a way to reference the key of the array instead of the value, likely by altering the return value of your function.

尬尬 2024-08-24 01:49:52

取消设置引用不会取消设置所引用的变量,这是预期的行为。一种解决方案是返回键而不是值,并使用它来取消设置原始值。

It is expected behavior that unsetting a reference does not unset the variable being referenced. One solution is to return the key instead of the value, and using that to unset the original value.

猫性小仙女 2024-08-24 01:49:52

请注意,unset 对引用的行为< /a> 是设计使然。您可以返回要删除的元素的索引,或者返回索引数组(如果数组不平坦)。

例如,您可以使用以下函数:

function delItem(&$array, $indices) {
    $tmp =& $array;
    for ($i=0; $i < count($indices)-1; ++$i) {
        $key = $indices[$i];
        if (isset($tmp[$key])) {
            $tmp =& $tmp[$key];
        } else {
            return array_slice($indices, 0, $i+1);
        }
    }
    unset($tmp[$indices[$i]]);
    return False;
}

或者,如果您更喜欢异常,

function delItem(&$array, $indices) {
    $tmp =& $array;
    while (count($indices) > 1) {
        $i = array_shift($indices);
        if (isset($tmp[$i])) {
            $tmp =& $tmp[$i];
        } else {
            throw new RangeException("Index '$i' doesn't exist in array.");
        }
    }
    unset($tmp[$indices[0]]);
}

Note that the behavior of unset on references is by design. You could instead return the index of the element to remove, or an array of indices if the array isn't flat.

For example, you could use the following function:

function delItem(&$array, $indices) {
    $tmp =& $array;
    for ($i=0; $i < count($indices)-1; ++$i) {
        $key = $indices[$i];
        if (isset($tmp[$key])) {
            $tmp =& $tmp[$key];
        } else {
            return array_slice($indices, 0, $i+1);
        }
    }
    unset($tmp[$indices[$i]]);
    return False;
}

Or, if you prefer exceptions,

function delItem(&$array, $indices) {
    $tmp =& $array;
    while (count($indices) > 1) {
        $i = array_shift($indices);
        if (isset($tmp[$i])) {
            $tmp =& $tmp[$i];
        } else {
            throw new RangeException("Index '$i' doesn't exist in array.");
        }
    }
    unset($tmp[$indices[0]]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文