I have function that returns the following multidimensional array. I don't have control of how the array is formed. Im trying to access the 'Result' elements. This issue is, the name of the parent elements constantly changing. The location of the 'Result' element is always the same (as the is the name "Result"). Is it possible to access that element without know the name of the parent elements?
Array
(
[sHeader] => Array
(
[aAction] => ActionHere
)
[sBody] => Array
(
[CreatePropertyResponse] => Array
(
[CreatePropertyResult] => Array
(
[Message] => Successfully completed the operation
[Result] => 0
[TransactionDate] => 2013-05-19T21:54:35.765625Z
[bPropertyId] => 103
)
)
)
)
An easy option to search the array keys/values recursively is to use a recursive iterator; these are built-in classes, part of the Standard PHP Library.
$result = false;
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach ($iterator as $key => $value) {
if ($key === 'Result') {
$result = $value;
break;
}
}
var_dump($result);
The bonus here is that you could, if you wanted to, check the depth of the Result item ($iterator->getDepth()) in the array structure and/or check one or more ancestor keys ($iterator->getSubIterator(…)->key()).
Array
(
[sHeader] => Array
(
[aAction] => ActionHere
)
[sBody] => Array
(
[CreatePropertyResponse] => Array
(
[CreatePropertyResult] => Array
(
[Message] => Successfully completed the operation
[Result] => 0
[TransactionDate] => 2013-05-19T21:54:35.765625Z
[bPropertyId] => 103
)
)
)
)
An easy option to search the array keys/values recursively is to use a recursive iterator; these are built-in classes, part of the Standard PHP Library.
$result = false;
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach ($iterator as $key => $value) {
if ($key === 'Result') {
$result = $value;
break;
}
}
var_dump($result);
The bonus here is that you could, if you wanted to, check the depth of the Result item ($iterator->getDepth()) in the array structure and/or check one or more ancestor keys ($iterator->getSubIterator(…)->key()).
0 comments:
Post a Comment