可能看不到東西, 我有兩個陣列:
$grid = Array ( [0] => 3 [1] => 2 [2] => 3 [3] => 2 ) $elements = Array ( [0] => 24426 [1] => 25015 [2] => 24422 [3] => 24425 [4] => 24531 [5] => 24421 [6] => 24530 [7] => 24532 [8] => 25016 [9] => 24418 )
基本上,這個想法是為 $grid 的每個值和 $elements 的值提供類似的東西。例如 [0] => 3 循環三次將得到 24426,25015,24422。現在問題來了,對於第二個結果 [1] => 2 我只需要取得兩個值,但不包含迭代的三個 $element 的先前值。所以基本上在第二次迭代中我會得到24425,24531。
注意:$grid 值可以是 1 , 2 ,3 ....300...n;
結果數組應該是這樣的:
Array ( [0] => 3,24426 [1] => 3,25015 [2] => 3,24422 [3] => 2,24425 [4] => 2,24531 [5] => 3,24421 [6] => 3,24530 [7] => 3,24532 [8] => 2,25016 [9] => 2,24418 )
P粉6676492532024-03-23 09:05:35
編輯:稍微更改程式碼以滿足所需的輸出格式
請考慮此程式碼。
$grid = [3, 2, 3, 2]; $elements = [24426,25015,24422,24425,24531,24421,24530,24532,25016,24418]; $result = []; foreach($grid as $take) { $org_take = $take; while($take-- > 0) { if (empty($elements)) { throw new Exception('Not enough elements'); } $result[] = sprintf('%d,%d', $org_take, array_shift($elements)); } } print_r($result);
給出結果:
Array ( [0] => 3,24426 [1] => 3,25015 [2] => 3,24422 [3] => 2,24425 [4] => 2,24531 [5] => 3,24421 [6] => 3,24530 [7] => 3,24532 [8] => 2,25016 [9] => 2,24418 )