过滤数组并添加缺失键的值

I'm having a bit of difficulty with something. I am trying to take an XML file and turn it into an XLS file using PhpSpreadSheet.

I am looping over the XML file just fin and appending the required data into an array and at the end of each item, I am pushing that array to the data array to get something like this.

foreach ( $namespaces as $namespace ) {
    foreach ( (array) $item->children( $namespace ) as $key => $child ) {
        if ( is_object( $child ) || is_array( $child ) ) {
            foreach ( (array) $child as $childKey => $childChild ) {
                $_item[ $key . '__' . $childKey ] = is_numeric( $childChild ) ? (float) $childChild : (string) $childChild;
            }
        } else {
            $_item[ $key ] = (string) $child;
        }
    }
}
$workSheetData[] = $_item;

I also build a single dimensional array where the keys and values are the same to use as the headers for my XLS file. This array $headers is being appended to every iteration to add any new keys I might come across.

$_worksheetHeaders = array_combine(array_keys($_item),array_keys($_item));
$worksheetHeaders  += $_worksheetHeaders;

Now I have certain rows that have multiple columns of something and these all have their own keys but since not all rows have all the keys I need to append the missing keys from the headers array to the right 2nd level array of $workSheetData with an empty string.

Furthermore since the header is being appended to I need to sort the dataArray in the same way as the headers so the right data ends up in the right column..

Any help would be appreciated.