我从类似so 的SimpleXML对象构建了一个类似so的数组
$currentXML = new 'SimpleXMLElement($getCurrentXML);
$leadArray = array();
foreach($currentXML->Job->Employee as $object) {
$dataArray[] = array(
'ID' => (string)$object->ID,
'Date Identified' => $dateIdentified,
'Name' => (string)$object->Name,
'Owner' => (string)$object->Owner->Name,
'Value' => (string)$object->EstimatedValue
);
}
如果我输出上面的数据,我会得到这样的东西,这是完美的
array:8 [▼
0 => array:7 [▼
"ID" => "1230279"
"Date Identified" => "19/04/2016"
"Name" => "Some Name"
"Owner" => "Some Owner"
"Value" => "Some Value"
]
1 => array:7 [▶]
2 => array:7 [▶]
3 => array:7 [▶]
4 => array:7 [▶]
5 => array:7 [▶]
6 => array:7 [▶]
7 => array:7 [▶]
]
现在,在上面的foreach循环中,我需要使用$object->ID来查询另一个API。所以我有
foreach($currentXML->Job->Employee as $object) {
$dataArray[] = array(
'ID' => (string)$object->ID,
'Date Identified' => $dateIdentified,
'Name' => (string)$object->Name,
'Owner' => (string)$object->Owner->Name,
'Value' => (string)$object->EstimatedValue
);
$customField = Helper::getCustomFields((string)$object->ID);
$currentFieldsXML = new 'SimpleXMLElement($customField);
}
现在,如果我输出currentFieldsXML,有时会返回并清空SimpleXMLElement对象,有时它包含数据。我想做的是将数据与其他数据一起推送到数组中。所以我有这个
foreach($currentXML->Job->Employee as $object) {
$dataArray[] = array(
'ID' => (string)$object->ID,
'Date Identified' => $dateIdentified,
'Name' => (string)$object->Name,
'Owner' => (string)$object->Owner->Name,
'Value' => (string)$object->EstimatedValue
);
$customField = Helper::getCustomFields((string)$object->ID);
$currentFieldsXML = new 'SimpleXMLElement($customField);
if(!empty($currentFieldsXML->CustomFields)) {
foreach($currentFieldsXML->CustomFields as $custom) {
array_push($dataArray, (string)$custom->CustomField->ID);
array_push($dataArray, (string)$custom->CustomField->Name);
array_push($dataArray, (string)$custom->CustomField->Text);
}
}
}
问题是输出类似于
array:23 [▼
0 => array:7 [▶]
1 => array:7 [▶]
2 => "122156"
3 => "Some Data"
4 => "Some more Data"
5 => array:7 [▶]
6 => "122156"
7 => "Date"
8 => "20 April"
]
因此,元素0没有与其关联的自定义字段。元素1确实有数据,但它显示为元素2、3和4。从本质上讲应该看起来像这个
array:8 [▼
0 => array:7 [▶]
1 => array:7 [▼
"ID" => "1230279"
"Date Identified" => "19/04/2016"
"Name" => "Some Name"
"Owner" => "Some Owner"
"Value" => "Some Value"
array:3 [
1 => "122156"
2 => "Some Data"
3 => "Some more Data"
]
]
...
]
那么,我如何将返回的数据添加到其相应的数组中,如上所示?
感谢
更新对不起,我的错误
$iterator=0;
foreach($currentXML->Job->Employee as $object) {
$dataArray[$iterator] = array(
'ID' => (string)$object->ID,
'Date Identified' => $dateIdentified,
'Name' => (string)$object->Name,
'Owner' => (string)$object->Owner->Name,
'Value' => (string)$object->EstimatedValue
);
$customField = Helper::getCustomFields((string)$object->ID);
$currentFieldsXML = new 'SimpleXMLElement($customField);
if(!empty($currentFieldsXML->CustomFields)) {
$seconditerator=0;
foreach($currentFieldsXML->CustomFields as $custom) {
$dataArray[$iterator][$custom->CustomField->ID][$seconditerator]=(string)$custom->CustomField->ID;
$dataArray[$iterator][$custom->CustomField->ID][$seconditerator]=(string)$custom->CustomField->Name;
$dataArray[$iterator][$custom->CustomField->ID][$seconditerator]=(string)$custom->CustomField->Text;
$seconditerator++;
}
}
$iterator++;
}