文章詳情頁
一維數(shù)組分割成二維數(shù)組
瀏覽:133日期:2022-06-02 15:33:02
問題描述
[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]
]
這段程序應(yīng)該怎么寫
問題解答
回答1:方法蠢了點(diǎn),但是能達(dá)到你想要的效果,希望可以幫到你,代碼如下: 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) { if ($value>= 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) // } //}上一條:能不能出一集你的git的安裝教程下一條:登錄功能報(bào)錯
相關(guān)文章:
1. 數(shù)組按鍵值封裝!2. java - 阿里的開發(fā)手冊中為什么禁用map來作為查詢的接受類?3. docker不顯示端口映射呢?4. javascript - webpack中alias配置中的“@”是什么意思?5. javascript - 使用vue官方腳手架進(jìn)行單元測試,如何覆蓋到watch里的變量?6. javascript - 為什么創(chuàng)建多行多列的表格最后只有一行內(nèi)有表格7. clone - git sourceTree克隆倉庫時(shí),都不停彈出Password Required彈窗,即時(shí)輸入正確的git賬號密碼還是彈出8. 如何用Java向kafka發(fā)送json數(shù)據(jù)9. 主題切換問題,用過別人的webapp在后臺切換模板主題后手機(jī)端打開網(wǎng)頁就是切換到的主題了10. html5 - 使用echarts中的圖表 一個(gè)頁面導(dǎo)入了好幾個(gè)js圖表 實(shí)現(xiàn)echarts圖表隨著瀏覽器窗口變化而變化時(shí)出現(xiàn)了問題
排行榜

熱門標(biāo)簽