dq_1984 2018-08-28 12:34
浏览 10

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

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP