php递归全局变量? [英] php recursion global variable?

查看:35
本文介绍了php递归全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个递归函数,它用一个全局变量解析一个对象/数组.如果我注释掉全局变量,我什么也得不到,但是如果我把它留在里面,它会继续向数组添加其他应该在它自己的结果集中的值.我需要在这里更改一些东西吗?

I have a recursion function that parses an object/array with a global variable. If I comment out the global variable I get nothing but if I leave it in it keeps adding to the array other values that should be in it own result set. Do I need to change something here?

更新#2:我怎样才能得到我想要的回报,我以为我把所有的唯一值都推到了数组中?

UPDATE #2: How can I get the return I want, I thought I was pushing all unique values to the array?

function getResp($objectPassed) {

    foreach($objectPassed as $element) {
        if(is_object($element)) {
            // recursive call
            $in_arr = getResp($element);
        }elseif(is_array($element)) {
            $in_arr = getResp($element);
        } else {
            // XML is being passed, need to strip it
            $element = strip_tags($element); 

            // Trim whitespace
            $element = trim($element);

            // Push to array
            if($element != '') {
                if (!preg_match("/^[0-9]$/", $element)) {
                    if (!in_array($element,$in_arr)) {
                        $in_arr[] = $element;
                    }
                }
            } 
        }
    }
    return $in_arr;
}

输入:

stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => typeName
                    [Id] => Array
                        (
                            [0] => a0E50000002jxhmEAA
                            [1] => a0E50000002jxhmEAA
                        )

                )

            [1] => stdClass Object
                (
                    [type] => typeName
                    [Id] => Array
                        (
                            [0] => a0E50000002jxYkEAI
                            [1] => a0E50000002jxYkEAI
                        )

                )

        )

    [size] => 2
)

返回:

Array
(
    [0] => a0E50000002jxYkEAI
)

想要的回报:

Array
(
    [0] => a0E50000002jxYkEAI
    [1] => a0E50000002jxhmEAA
)

推荐答案

是否需要全局变量?否则你可以这样简化它:

Is a global variable necessary? Otherwise you could simplify it this way:

function getResp($objectPassed, &$in_arr = array()) {  // <-- note the reference '&'

    foreach($objectPassed as $element) {
        if(is_object($element) || is_array($element)) { // <-- else if statement simplified
            getResp($element,$in_arr);
        } else {
            // XML is being passed, need to strip it
            $element = strip_tags($element); 

            // Trim whitespace
            $element = trim($element);

            // Push to array  
            if($element != ''  &&                      // <-- everything in one test
              !preg_match("/^[0-9]$/", $element) &&
              !in_array($element,$in_arr)) 
            {                    
                  $in_arr[] = $element;  
            } 
        }
    }
    return $in_arr;
}

然后你做:

$result = getResp($data);

如果递归函数必须一遍又一遍地访问相同的资源(在这种情况下是初始数组),我总是将此作为参考传递.
我不知道是否可以衡量,但我猜这比复制值更有效.

If a recursive function has to access the same resource over and over again (in this case the initial array), I would always pass this as a reference.
I don't know if is measurable but I would guess that this is much more efficient than copying values.

这篇关于php递归全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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