一维数组分割成二维数组
看我的大白眼
看我的大白眼 2021-04-21 13:53:53
0
1
794

[0.6,0.7,1,0,-1,-2,-3,0,0,-1,-5,-6,1,3,4]

分割成

[

[0.6,0.7,1],

[0],

[-1,-2,-3],

[0,0],

[-1,-5,-6],

[1,3,4]

]

这段程序应该怎么写

看我的大白眼
看我的大白眼

Antworte allen(1)
手机用户1616837104

方法蠢了点,但是能达到你想要的效果,希望可以帮到你,代码如下: public function index() { $array = [0.6, 0.7, 1, 0, -1, -2, -3, 0, 0, -1, -5, -6, 1, 3, 4]; $result = []; foreach ($array as $value) { $count = count($result); if ($count > 0) { $single = $result[$count - 1]; $index = end($single); if (0 === $index) { if (0 === $value) { $result = $this->handleArray($result, $single, $value, $count); } else { $result = $this->handleArrayTwo($result, $value); } } elseif ($index > 0) { if ($value handleArrayTwo($result, $value); } else { $result = $this->handleArray($result, $single, $value, $count); } } elseif ($index = 0) { $result = $this->handleArrayTwo($result, $value); } else { $result = $this->handleArray($result, $single, $value, $count); } } } else { $result = $this->handleArrayTwo($result, $value); } } var_dump($result); die; } private function handleArray($result, $single, $value, $index) { unset($result[$index - 1]); array_push($single, $value); array_push($result, $single); return array_values($result); } private function handleArrayTwo($result, $value) { $array = []; array_push($array, $value); array_push($result, $array); return $result; } // array(6) { // [0]=> // array(3) { // [0]=> // float(0.6) // [1]=> // float(0.7) // [2]=> // int(1) // } // [1]=> // array(1) { // [0]=> // int(0) // } // [2]=> // array(3) { // [0]=> // int(-1) // [1]=> // int(-2) // [2]=> // int(-3) // } // [3]=> // array(2) { // [0]=> // int(0) // [1]=> // int(0) // } // [4]=> // array(3) { // [0]=> // int(-1) // [1]=> // int(-5) // [2]=> // int(-6) // } // [5]=> // array(3) { // [0]=> // int(1) // [1]=> // int(3) // [2]=> // int(4) // } //}

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!