我知道我可以循环,通过每个级别的对象,但是我想一个更简单的方法。

QueryResult Object
(
    [queryLocator] => 
    [done] => 1
    [records] => Array
        (
            [0] => SObject Object
                (
                    [type] => type_1
                    [fields] => 
                    [sobjects] => Array
                        (
                            [0] => SObject Object
                                (
                                    [type] => type_2
                                    [fields] => 
                                    [sobjects] => Array
                                        (
                                            [0] => SObject Object
                                                (
                                                    [type] => type_3
                                                    [fields] => 
                                                    [sobjects] => Array
                                                        (
                                                            [0] => SObject Object
                                                                (
                                                                    [type] => type_4
                                                                    [fields] => 
                                                                    [sobjects] => Array
                                                                        (
                                                                            [0] => SObject Object
                                                                                (
                                                                                    [type] => type_5
                                                                                    [fields] => 
                                                                                    [Id] => 12345_I_need_this
                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [size] => 1
)

我需要这个Id值的type_5,我怎么会得到这一简单的解决方案。

一些其他观点认为关于:

  • 我怎么会不知道大多深的目的阵列会去的,可能或多或少于5

我有听说过递归但没发现什么我认为我可以使用,保持它的简单。也许有些更好的教程将帮助我。如果我知道在什么部分列目的价值,我是内德在可能我只是叫它直接?是这样的:$目[5]->id???

有帮助吗?

解决方案

这里是怎么递归的作品(一般)

function recursiveFunctionName( input ) // returns value;
{
    //Do something to input to make it new_input

    if( isSomethingAccomplished )
    {
        return value;
    }
    else
    {
        return recursiveFunctionName( new_input );
    }
}

你开始的输入,和你告诉功能继续呼吁本身,直到可以返回一个有效的输出。在你的情况,你可以做这种方式:

function getID( SObject $so )
{
    // equates to isSomethingAccomplished...  You have found the value
    // you want returned, so pass that out.
    if( $so->id )
    {
        return $so->id;
    }
    else
    {
        // otherwise, this will return the value from the next level, 
        // pass that out.
        # SEE BELOW FOR ONE MORE NOTE HERE.
        return getID( $so->sobjects[ 0 ] );
    }
}

现在,因为你使用一系列为sobjects,你可能想要替换线以下#见下文如下:

$objs  = $so->sobjects;
$count = count( $objs );

// Iterate through all of its children, testing each of the child nodes.
// (You're actually using iteration and recursion in combination here).
for( $i = 0; $i < $count; $i++ )
{
    $curr = getID( $objs[ $i ] );

    // This is the same test as above.
    if( $curr )
    {
        return $curr;
    }
}

其他提示

这是非常简单的:

class SObject{
/*...*/

    public getId(){
        if(isset($this->Id)){
            return $this->Id;
        } else {
            return $this->sobjects[0]->getId();
        }
    }
}

和你通话

$id = $query_obj->getId();

放弃这样一种XML和使用XPATH在这如果你需要做很多的queryes在这种结构

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top