PHPを簡単に得る値からマルチレベルのオブジェクト
-
16-09-2019 - |
質問
思いでループを通じて各レベルのオブジェクト、いつづけたいと思っていますより簡単なアプローチ。
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
と聞き再帰がhavnが見つからないものと思い利用については、次のとおりであります。もしかしたよりよいチュートリアルが助けてくれます。いったいどの部分配列のオブジェクトの価値を私市にあったもんですか?のようなもの:$object[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の配列を使用しているので、さて、あなたは次のようには、以下の#SEE以下の行を交換することがあります:
$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としてこれをダンプし、この構造にqueryesの多くを行う必要がある場合、それにXPATHを使用する
所属していません StackOverflow