kate_hudson kate_hudson - 7 months ago 10
PHP Question

adding additional data to array

I build up an array like so from a SimpleXML Object like 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
);
}


If I output the data for the above, I get something like this which is perfect

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 [▶]
]


Now within the above foreach loop, I need to use $object->ID to query another API. SO I have

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);
}


Now if I output currentFieldsXML, sometimes I am returned and empty SimpleXMLElement Object, sometimes it contains data.
What I am trying to do is push the data into the array alongside its other data. So I have this

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);
}
}
}


The problem with this is that the output is something like this

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"
]


So element 0 has no custom fields associated with it. Element 1 does have data, but it is displaying as element 2, 3 and 4. In essence, the above
should look something like this

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"
]
]
...
]


So how can I add the data I am returned into its appropiate array as shown above?

Thanks

UPDATE
Sorry, my mistake

Answer
$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]['newarray'][$seconditerator]=(string)$custom->CustomField->ID;
            $dataArray[$iterator]['newarray'][$seconditerator]=(string)$custom->CustomField->Name;
            $dataArray[$iterator]['newarray'][$seconditerator]=(string)$custom->CustomField->Text;
            $seconditerator++;
         }
     }
     $iterator++;
}
Comments