0

嗨,我有一个看起来像这样的数组

数组 ( [0] => T [1] => T [2] => T [3] => T [4] => T [5] => T [6] => T [7] => T [8] => T [9] => T [10] => T [11] => T [12] => T [13] => T [14] => T [15] => T [16 ] => T [17] => T [18] => T [19] => T [20] => T [21] => T [22] => T [23] => T [24] = > T [25] => T [26] => T [27] => T [28] => T [29] => T [30] => T [31] => T [32] => T [33] => T [34] => T [35] => T [36] => T [37] => T [38] => T [39] => T [40] => T [41 ] => T [42] => T [43] => T [44] => T [45] => T [46] => T [47] => T [48] => T [49] = > T [50] => T [51] => T [52] => T [53] => T [54] => T [55] => T [56] => T [57] => T [58] => T [59] => T [60] => T [61] => T [62] => T [63] => T [64] => T [65] =>T [66] => T [67] => T [68] => T [69] => T [70] => T [71] => T [72] => 0 [73] => 0 [ 74] => 0 [75] => 0 [76] => 0 [77] => 0 [78] => 0 [79] => 0 [80] => 0 [81] => 0 [82] => 0 [83] => 0 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 0 [97] => 0 [98] => 0 [ 99] => 0)0 [98] => 0 [99] => 0)0 [98] => 0 [99] => 0)

我需要做的是将键 [0]-[49]、[50]-[99] 和 [100]-[149] 分组到三个字符串变量中,以便它们可以插入数据库字段 'answ1-50 '、'answ51-100' 和 'answ101-150。我对这个有点迷茫,因为我还是一个 php 新手。我如何将这个数组分成三组作为字符串?

4

3 回答 3

2

虽然不是好的做法,但您发布的问题的答案是:

list($group1, $group2, $group3) = array_map("implode", array_chunk($arr, 50));

(假设您总是有 150 个答案。如果没有,请跳过该列表,您将获得一组结果 $groups[0]、$groups[1]、$groups[2])

于 2013-06-13T19:48:26.573 回答
0

我不确定您为什么要这样做,但这应该可以解决问题。

//assuming content in $myarray
$newarr = array();
for($i = 0; $i < count($myarray); $i++ {
    $j = floor($i / 50);
    if($j == $i / 50) {
        $newarr[$j] = "";
    }
    $newarr[$j] += (string)$myarray[$i]; 
}

$newarr 的每个元素都应该是一个包含 50 个答案的字符串。

于 2013-06-13T19:43:58.887 回答
0

您可以简单地将它写在循环下并将值连接到一个字符串。

for($i = 0; $i < count($yourarray); $i++) {
if(i>=0 && $i<50)
{
    $str1=$str1.$yourarray[i];
}
else if(i>=50 && $i<100)
{
    $str2=$str2.$yourarray[i];
}
else if(i>=100 && $i<150)
{
    $str3=$str3.$yourarray[i];
}}
于 2016-02-15T23:29:25.450 回答