我知道我可以遍历对象的每个级别,但我想要一种更简单的方法。
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值,我怎么能在一个简单的解决方案中得到它。
要考虑的其他一些要点:
我听说过递归但是没有找到任何我认为可以使用的东西,这样可以保持简单。也许一些更好的教程会帮助我。 如果我确实知道对象数组的哪一部分我可以直接调用它?类似于:$ object [5] - > id ???
答案 0 :(得分:2)
这很简单:
class SObject{
/*...*/
public getId(){
if(isset($this->Id)){
return $this->Id;
} else {
return $this->sobjects[0]->getId();
}
}
}
你打电话
$id = $query_obj->getId();
答案 1 :(得分: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 ] );
}
}
现在,由于您正在使用数组进行对象,您可能需要使用以下内容替换#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;
}
}
答案 2 :(得分:1)
如果您需要在此结构上进行大量查询,请将其转储为XML并在其上使用XPATH