public $nav = [1 => '主导航', 2 => '侧导航', 3 => '底部导航']; public function translat($arr) { $new_arr = []; if (count($arr) == count($arr, 1)) { $arr->navtype = $this->nav[$arr->navtype]; $new_arr = $arr; } else { foreach ($arr as $val) { $val->navtype = $this->nav[$val->navtype]; $new_arr[] = $val; } } return $new_arr; }
要学会多去用数组解决问题,在循环中foreach的速度是最快的,中间是for,switch是最慢的