我从像SimpleXML对象那样建立一个像这样的数组
$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"
]
]
...
]
那么如何将我返回的数据添加到其适当的数组中,如上所示?
由于
更新 对不起,我的错误
答案 0 :(得分:1)
$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++;
}