[英]adding additional data to array
我从像这样的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"
]
]
...
]
那么如何将返回的数据添加到上面所示的适当数组中?
谢谢
更新抱歉,我的错误
$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++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.