所以,我尝试过使用array_slice,或者我不知道我在做什么,或者我需要另一个解决方案。我有一个从数据库返回的长数组。我需要将其分解为连续六个元素的较小数组,以便使用正确的项目应用正确的价格。我意识到许多数组元素中没有数据。那没问题。这些代表了特定商品的特定尺寸的价格。 如果有人能指出我正确的方向,我将不胜感激。这是来自print_r的我的数组。
Array ( [0] => [1] => [2] => [3] => 761 [4] => [5] => [6] => [7] => [8] => [9] => [10] => 823 [11] => 937 [12] => [13] => [14] => [15] => 701 [16] => [17] => [18] => [19] => [20] => [21] => [22] => 745 [23] => 854 [24] => [25] => [26] => [27] => 735 [28] => [29] => [30] => [31] => [32] => [33] => [34] => 985 [35] => 1067 [36] => [37] => [38] => [39] => 655 [40] => [41] => [42] => [43] => [44] => [45] => 658 [46] => [47] => [48] => [49] => [50] => [51] => [52] => 701 [53] => 807 [54] => [55] => [56] => [57] => 692 [58] => [59] => [60] => [61] => [62] => [63] => [64] => [65] => 1020 [66] => [67] => [68] => 1039 [69] => [70] => 1099 [71] => [72] => [73] => [74] => 1029 [75] => [76] => 1149 [77] => [78] => 659 [79] => 664 [80] => [81] => [82] => [83] => [84] => 714 [85] => [86] => [87] => [88] => [89] => [90] => [91] => 724 [92] => 724 [93] => 739 [94] => 759 [95] => [96] => [97] => [98] => [99] => [100] => 989 [101] => [102] => 599 [103] => [104] => [105] => [106] => [107] => [108] => [109] => 679 [110] => 674 [111] => 669 [112] => 689 [113] => [114] => [115] => [116] => [117] => [118] => 899 [119] => [120] => 599 [121] => [122] => [123] => [124] => [125] => [126] => [127] => 614 [128] => 609 [129] => 639 [130] => 634 [131] => [132] => [133] => [134] => [135] => [136] => 834 [137] => [138] => 584 [139] => [140] => [141] => [142] => [143] => [144] => [145] => 652 [146] => 659 [147] => 644 [148] => 649 [149] => [150] => [151] => [152] => [153] => [154] => 829 [155] => [156] => 674 [157] => [158] => [159] => [160] => [161] => [162] => [163] => 659 [164] => 774 [165] => 679 [166] => 719 [167] => 969 [168] => [169] => [170] => [171] => [172] => 994 [173] => [174] => [175] => [176] => [177] => 1100 [178] => [179] => [180] => [181] => [182] => [183] => [184] => 1215 [185] => [186] => [187] => [188] => [189] => [190] => [191] => 1334 [192] => [193] => [194] => [195] => 825 [196] => [197] => [198] => [199] => [200] => [201] => [202] => 898 [203] => 1045 [204] => [205] => [206] => [207] => 915 [208] => [209] => [210] => [211] => [212] => [213] => [214] => 1012 [215] => [216] => [217] => [218] => [219] => [220] => [221] => 1174 [222] => [223] => [224] => [225] => 963 [226] => [227] => 1224 [228] => [229] => [230] => [231] => 944 [232] => [233] => 1222 [234] => [235] => [236] => [237] => 819 [238] => [239] => [240] => [241] => [242] => [243] => 822 [244] => [245] => [246] => [247] => [248] => [249] => [250] => 891 [251] => 1016 [252] => [253] => [254] => [255] => 856 [256] => [257] => [258] => [259] => [260] => [261] => [262] => 1131 [263] => 1229 [264] => [265] => [266] => [267] => [268] => [269] => 1234 [270] => 972 [271] => [272] => [273] => [274] => [275] => [276] => [277] => [278] => 982 [279] => [280] => 1017 [281] => [282] => [283] => 937 [284] => 932 [285] => [286] => 947 [287] => [288] => 857 [289] => [290] => [291] => [292] => [293] => [294] => [295] => [296] => 867 [297] => [298] => 892 [299] => [300] => [301] => [302] => 917 [303] => [304] => 907 [305] => [306] => [307] => [308] => 1032 [309] => [310] => 977 [311] => [312] => [313] => [314] => [315] => [316] => 1307 [317] => [318] => [319] => [320] => [321] => [322] => 1182 [323] => [324] => [325] => [326] => [327] => [328] => 1322 [329] => [330] => [331] => [332] => [333] => 1019 [334] => [335] => [336] => [337] => [338] => [339] => [340] => [341] => 1265 [342] => [343] => [344] => [345] => 1019 [346] => [347] => [348] => [349] => [350] => [351] => [352] => 1093 [353] => 1265 )
答案 0 :(得分:3)
这应该适合你:
<?php
//As an example
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
//Splits the array into groups of 6
$array = array_chunk($array, 6);
print_r($array);
?>
输出:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
[1] => Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 10
[4] => 11
[5] => 12
)
)