【发布时间】:2019-11-22 19:51:31
【问题描述】:
我想将 XML 数据导入我的数据库。但是我很难循环它,因为数组不是同一层。
我试过foreach,但有些数据没有插入,虽然每个数据的键是一样的。
这是当前的输出数组
array:3 [▼
0 => array:4 [▼
"BANFN" => "001"
"BNFPO" => "00010"
"LIFNR_ID" => "0000001244"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
1 => array:4 [▼
"BANFN" => "0011023036"
"BNFPO" => "00010"
"LIFNR_ID" => "0000001244"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
2 => array:2 [▼
0 => array:4 [▼
"BANFN" => "0011022474"
"BNFPO" => "00010"
"LIFNR_ID" => "0000003468"
"STATUS" => array:2 [▼
"MSGTYP" => "E"
"MSG" => "random text"
]
]
1 => array:4 [▼
"BANFN" => "0011022474"
"BNFPO" => "00010"
"LIFNR_ID" => "0000003468"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
]
]
这是我想要实现的代码
array:3 [▼
0 => array:4 [▼
"BANFN" => "0011023037"
"BNFPO" => "00010"
"LIFNR_ID" => "0000001244"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
1 => array:4 [▼
"BANFN" => "0011023036"
"BNFPO" => "00010"
"LIFNR_ID" => "0000001244"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
2 => array:4 [▼
"BANFN" => "0011023036"
"BNFPO" => "00010"
"LIFNR_ID" => "0000001244"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
3 => array:4 [▼
"BANFN" => "0011023036"
"BNFPO" => "00010"
"LIFNR_ID" => "0000001244"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
4 => array:4 [▼
"BANFN" => "0011023036"
"BNFPO" => "00010"
"LIFNR_ID" => "0000001244"
"STATUS" => array:2 [▼
"MSGTYP" => "S"
"MSG" => "random text"
]
]
]
我需要用键将同一层上的数组展平。但我想不通。或者是否有任何用于将所有数组插入数据库的 foreach 或循环方法?
创建原始数组的代码是
$url = Storage::disk('ftp')->allFiles('PM/PR_RFQNUM/');
$v = [];
foreach ($url as $key => $files) {
//$file[] = $files;
$xml = Storage::disk('ftp')->get($files);
$file = Formatter::make($xml, Formatter::XML)->toArray();
foreach($file as $k=>$value){
$v[] = $value;
}
}
【问题讨论】:
-
你如何得到你当前的数组,你处理的数据是什么?
-
这是我的 foreach
$url = Storage::disk('ftp')->allFiles('PM/PR_RFQNUM/'); $v = []; foreach ($url as $key => $files) { //$file[] = $files; $xml = Storage::disk('ftp')->get($files); $file = Formatter::make($xml, Formatter::XML)->toArray(); foreach($file as $k=>$value){ $v[] = $value; } }'@NigelRen