0

我知道我可以遍历对象的每个级别,但我想要一种更简单的方法。

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
)

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

需要考虑的其他几点:

  • 我不知道数组的对象会有多大或多深,可能大于或小于 5

我听说过递归,但没有找到任何我认为可以使用的东西来保持简单。也许一些更好的教程会帮助我。如果我确实知道我需要的值在对象数组的哪个部分,我可以直接调用它吗?类似于:$object[5]->id ???

4

3 回答 3

2

这很简单:

class SObject{
/*...*/

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

你打电话给

$id = $query_obj->getId();
于 2009-09-25T15:13:02.560 回答
2

这是递归的工作原理(通常)

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 ] );
    }
}

现在,由于您使用 Array 作为 sobjects,您可能希望将 #SEE BELOW 下面的行替换为以下内容:

$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;
    }
}
于 2009-09-25T17:14:59.137 回答
1

如果您需要对此结构进行大量查询,请将其转储为 XML 并在其上使用 XPATH

于 2009-09-25T16:59:23.630 回答