文章詳情頁
一維數(shù)組分割成二維數(shù)組
瀏覽:102日期: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:方法蠢了點,但是能達到你想要的效果,希望可以幫到你,代碼如下: 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的安裝教程下一條:登錄功能報錯
相關(guān)文章:
1. javascript - vue 移動端的input 數(shù)字輸入優(yōu)化2. java - mongodb分片集群下,count和聚合統(tǒng)計問題3. java - 自己制作一個視頻播放器,遇到問題,用的是內(nèi)置surfaceview類,具體看代碼!4. android - java 泛型不支持?jǐn)?shù)組,那么RxJava的Map集合有什么方便的手段可以定義獲得一串共同父類集合數(shù)據(jù)呢?5. 服務(wù)器端 - 采用nginx做web服務(wù)器,C++開發(fā)應(yīng)用程序 出現(xiàn)拒絕連接請求?6. 為什么我ping不通我的docker容器呢???7. 關(guān)于docker下的nginx壓力測試8. python - pandas按照列A和列B分組,將列C求平均數(shù),怎樣才能生成一個列A,B,C的dataframe9. javascript - 有什么兼容性比較好的辦法來判斷瀏覽器窗口的類型?10. java - 靜態(tài)屬性中的賦值和靜態(tài)代碼塊中的賦值有什么區(qū)別?
排行榜

熱門標(biāo)簽