Reputation: 717
Hi I have an array that looks something like this
Array ( [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 )
What I need to do is group keys [0]-[49], [50]-[99], and [100]-[149] into three string variables so tht they can be inserted into the database fields 'answ1-50', 'answ51-100', and 'answ101-150. Im a little bit lost on this one as I am still somewhat of a php novice. How do i break up this array into three groups as a string?
Upvotes: 0
Views: 238
Reputation: 23
You can just simply write it under the loop and concatenate the values to a string.
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];
}}
Upvotes: 0
Reputation: 1701
While not good practice, the answer to your posted question is to:
list($group1, $group2, $group3) = array_map("implode", array_chunk($arr, 50));
(assumes that you always have 150 answers. if you don't, skip the list and you will have an array of results $groups[0], $groups[1], $groups[2])
Upvotes: 2
Reputation: 861
I'm not sure why you want to do this, but this should do the trick.
//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];
}
Each element of $newarr should be a string with 50 answers in it.
Upvotes: 0