0
为了访问一些嵌套值(对于DB插入),我需要对大型数据集执行“深度循环”。访问没有太多foreach循环的嵌套数据
目前我有以下代码,它工作,但它是非常缓慢,并最终在所有数据完成处理之前超时。我无法弄清楚如何在没有这么多循环的情况下获得嵌套数据。
样本数组:
$resultArray = [
{
"item1": "value",
"item2": "value",
"item3": "value",
"item4": "value",
"item5": [
{
"anothervalue1": "value",
"anothervalue2": "value",
"anothervalue3": "value",
"anothervalue4": "value",
"anothervalue5": "value"
},
{
"anothervalue1": "value",
"anothervalue2": "value",
"anothervalue3": "value",
"anothervalue4": "value",
"anothervalue5": "value"
},
{
"anothervalue1": "value",
"anothervalue2": "value",
"anothervalue3": "value",
"anothervalue4": "value",
"anothervalue5": "value"
}
{
// another 150+
}
]
}
];
实施现状:
// Loop over main array
foreach ($resultArray as $object) {
// Loop over each object
foreach ($object as $key => $value) {
if ($key === 'item5') {
// Loop over each 'item5' object
foreach ($value as $history_key => $history_value) {
foreach ($history_value as $history_deep_key => $history_deep_value) {
$arr = array(
$_GET['someparam'], // column1 data
$object['item1'], // column2 data
$_GET['anotherparam'], // column3 data
$object['item2'], // column4 data
$history_value['anothervalue1'], // column5 data
$history_value['anothervalue2'], // column6 data
$history_value['anothervalue3'], // column7 data
$history_value['anothervalue4'], // column8 data
$history_value['anothervalue5'], // column9 data
$object['item3'] // column10 data
);
$statement = $link->prepare("INSERT INTO reporting_clients(column1, column2, column3, column4, column5, column6, column7, column8, column9, column10)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$statement->execute($arr);
}
}
}
}
}
是否有可能访问嵌套数据wihtout这么多foreach
循环?
如果您知道密钥。那么你可以直接访问数据。 –
如果您不知道密钥,那么这将是访问数据的最佳方式。也可以把它变成你需要重复使用它的功能。 'in_array'也可能会有帮助 – Austin
让我看看你的原始代码。所以基于这个我可以帮你 –