向数组添加额外的数据
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++;
}
我像这样从一个 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++;
}