在 PHP 中实现深度收集可以使用递归函数来实现。以下是一个简单的示例代码:
function deepCollect($array) {
$result = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, deepCollect($value));
} else {
$result[] = $value;
}
}
return $result;
}
$array = [
1,
2,
[3, 4, [5, 6]],
7,
[8, [9, 10]]
];
$result = deepCollect($array);
print_r($result);
在上面的示例中,deepCollect
函数接收一个多维数组作为参数,并使用递归遍历数组中的每个元素。如果元素是数组,则递归调用 deepCollect
函数并将结果合并到结果数组中;否则直接将元素添加到结果数组中。最终返回一个包含所有深度收集的元素的新数组。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:php exec如何处理用户满意度提升