参考检测阵列从另一个函数 [英] Reference detection in array from another function

查看:169
本文介绍了参考检测阵列从另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用的针法,但检测到一个水平太晚参考:

So I'm using the pin method, but the reference is detected one level too late:

$pin = time();

function wrap($arr){
  test($arr);
}

function test(&$arr){
    global $pin;

    if(in_array($pin, $arr))
        return print "ref";

    $arr[] = $pin;

    foreach($arr as &$v){

        if($v != $pin){

            if(is_array($v))
                return test($v);

            print $v . " ";

      }

    }

}

$array = array(1, 2, 3);
$array[4] = &$array;

wrap($array);

我得到 1 2 3 1 2 3 REC

但我希望 1 2 3 REC

如果我只是做试验($ ARR)那么它的工作原理,但问题是,我需要包装内的另外一个接受的值不是引用的测试功能: (

If I just do test($arr) then it works, but the problem is that I need to wrap the test function inside another one that accepts values not references :(

有什么办法,我可以用我的包装功能过检测在适当的时候参考?

Is there any way I can detect the reference at the right moment with my wrapper function too?

推荐答案

简介

我想一个更好的方法是创建数组的副本,并比较修改,而不是使用全局针,它仍然可以是一个 100%的递归

I think a better approach would be to create a copy of the array and compare modification rather than use global pin and it can still be a 100% Recursive

示例1

这是从你上面的例子:

$array = array(1,2,3);
$array[4] = &$array;
wrap($array);

输出

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [4] => ref
)

例2

难道我们真的确定其检测参考值或只是一个数组的副本

Are we really sure its detecting reference or just a copy of the array

//Case 1 : Expect no modification
$array = array(1, 2, 3, array(1, 2, 3));
wrap( $array);

//Case 2 : Expect Modification in Key 2
$array = array(1, 2, 3, array(1, 2, 3));
$array[2] = &$array;
wrap( $array);

输出

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)
Array
(
    [0] => 1
    [1] => 2
    [2] => ref
    [3] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

例3

这真的是递归?

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

输出

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [4] => Array
        (
            [4] => Array
                (
                    [2] => Array
                        (
                            [6] => Array
                                (
                                    [1] => Array
                                        (
                                            [0] => 1
                                            [1] => 2
                                            [3] => ref   <-- GOT YOU
                                        )

                                )

                        )

                )

        )

)

您的修改功能

/**
 * Added printf since test now returns array
 * @param array $arr
 */
function wrap(array $arr) {
    printf("<pre>%s<pre>", print_r(test($arr), true));
}


/**
 * - Removed Top Refrence
 * - Removed Global
 * - Add Recursion
 * - Returns array
 * @param array $arr
 * @return array
 */
function test(array $arr) {
    $temp = $arr;
    foreach ( $arr as $key => &$v ) {
        if (is_array($v)) {
            $temp[$key]['_PIN_'] = true;
            $v = isset($arr[$key]['_PIN_']) ? "ref" : test($v);
        }
    }
    unset($temp); // cleanup
    return $arr;
}

这篇关于参考检测阵列从另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆