方法实现
/** * 多维数组扁平化 * @param $array array * @return array */ if (!function_exists('array_flatten')) { function array_flatten($array) { $result = []; foreach ($array as $value) { if (is_array($value)) { $result = array_merge($result, array_flatten($value)); } else { $result[] = $value; } } return $result; } }
代码示例
<?php $arr = [ [ '1', '2' ], [ '1', '4', '5' ], ]; print_r(json_encode(array_flatten($arr))); // ["1","2","1","4","5"] // 去重处理,注意需要取值操作 print_r(json_encode(array_values(array_unique(array_flatten($arr))))); // ["1","2","4","5"]