我从SOAP Web服务获得这样的对象:
object(stdClass)#2 (1) {
["tasks"]=>
array(3) {
[0]=>
object(stdClass)#3 (9) {
["externalId"]=>
string(0) ""
["name"]=>
string(10) "Customer A"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["subtasks"]=>
array(2) {
[0]=>
object(stdClass)#4 (9) {
["externalId"]=>
string(1) "2"
["name"]=>
string(5) "Job 1"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["subtasks"]=>
object(stdClass)#5 (9) {
["externalId"]=>
string(0) ""
["name"]=>
string(7) "Job 1.1"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["subtasks"]=>
array(2) {
[0]=>
object(stdClass)#6 (8) {
["externalId"]=>
string(0) ""
["name"]=>
string(9) "Job 1.1.1"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
[1]=>
object(stdClass)#7 (8) {
["externalId"]=>
string(0) ""
["name"]=>
string(9) "Job 1.1.2"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
}
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
[1]=>
object(stdClass)#8 (9) {
["externalId"]=>
string(0) ""
["name"]=>
string(5) "Job 2"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["subtasks"]=>
object(stdClass)#9 (8) {
["externalId"]=>
string(2) "55"
["name"]=>
string(7) "Job 2.1"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
}
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
[1]=>
object(stdClass)#10 (8) {
["externalId"]=>
string(3) "123"
["name"]=>
string(10) "Customer B"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
[2]=>
object(stdClass)#11 (9) {
["externalId"]=>
string(0) ""
["name"]=>
string(10) "Customer C"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["subtasks"]=>
object(stdClass)#12 (8) {
["externalId"]=>
string(3) "918"
["name"]=>
string(5) "Job 1"
["description"]=>
string(0) ""
["bookable"]=>
bool(true)
["billable"]=>
bool(true)
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
["customField1"]=>
string(0) ""
["customField2"]=>
string(0) ""
["customField3"]=>
string(0) ""
}
}
}
我只需要一个愚蠢的函数来检查externalId的每个元素。
if( ["externalId"] != "")
{
echo "value found! ".["externalId"]."<br>";
}
我尝试使用array_walk_recursive
但该对象是数组和对象的混合。
比较[“子任务”] - 元素...... 还在SOAP调用中尝试了SOAP_SINGLE_ELEMENT_ARRAYS,没有区别。 有一些“魔力”吗?我认为解决方案很简单,但还没有得到它: - (
请记住:递归级别没有限制 - 每个[“子任务”]都可以包含更多[“子任务”]。
谢谢
答案 0 :(得分:0)
试试这个
<?php
/**
* @param \stdClass $root
*
* @return array
*/
function externalIdentifiersFrom(\stdClass $root)
{
static $properties = [
'tasks',
'subtasks',
];
$identifiers = [];
if (property_exists($root, 'externalId')
&& is_string($root->externalId)
&& '' !== trim($root->externalId)
) {
$identifiers[] = $root->externalId;
}
foreach ($properties as $property) {
if (property_exists($root, $property) && is_array($root->{$property})) {
$objects = $root->{$property};
foreach ($objects as $object) {
if (!$object instanceof \stdClass) {
continue;
}
$identifiers = array_merge(
$identifiers,
externalIdentifiersFrom($object)
);
}
}
}
return array_unique($identifiers);
}
var_dump(externalIdentifiersFrom($object));