PHP从数组+;对象
我从SOAP web服务中获得了一个类似这样的对象:PHP从数组+;对象,php,recursion,Php,Recursion,我从SOAP web服务中获得了一个类似这样的对象: object(stdClass)#2 (1) { ["tasks"]=> array(3) { [0]=> object(stdClass)#3 (9) { ["externalId"]=> string(0) "" ["name"]=> string(10) "Customer A" ["description"]=>
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>";
}
if([“externalId”]!=“”)
{
回显“找到值!”。“
”;
}
我尝试使用array\u walk\u recursive
,但对象是数组和对象的混合体
比较[“子任务”]-元素。。。
还尝试了SOAP调用中的SOAP\u单元素\u数组,没有任何区别。
有什么“魔力”吗?我认为解决方案很简单,但还不明白:-(
请记住:递归级别没有限制-每个[“子任务”]可以包含更多[“子任务”]
谢谢试试这个
<?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));