I have a string that contains the given answers from a qiuz by a user. This string is saved on the db table and it has this form.
,P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6
Ater getting the string i explode it, and the form becomes like this.
Array
(
[0] => P1
[1] => 1
[2] => 2
[3] => 3
[4] => 5
[5] => 8
[6] => 9
[7] => P2
[8] => 1
[9] => 3
[10] => 4
[11] => 5
[12] => 6
[13] => 8
[14] => 9
[15] => P3
[16] => 1
[17] => 2
[18] => 3
[19] => 4
[20] => 6
[21] => 8
[22] => 9
[23] => P4
[24] => 1
[25] => P5a
[26] => b
[27] => P5b
[28] => d
[29] => P5c
[30] => a
[31] => P5d
[32] => c
[33] => P6
[34] => 4
[35] => P7
[36] => 2
[37] => P8a
[38] => hc
[39] => P8b
[40] => df
[41] => P8c
[42] => bg
[43] => P8d
[44] => e
[45] => P9
[46] => 4
[47] => P10
[48] => 3
[49] => P11
[50] => 4
[51] => P12
[52] => 2
[53] => P13
[54] => 3
[55] => P14
[56] => 3
[57] => P15a
[58] => acejg
[59] => P15b
[60] => dfhib
[61] => P16
[62] => 1
[63] => 3
[64] => P17
[65] => 2
[66] => P18
[67] => 1
[68] => P19
[69] => 3
[70] => P20
[71] => 3
[72] => 5
[73] => 6
)
each question starts with a "P". So question 1 will be "P1" question 2 "P2" and so on.
Each given answer is between the "P" until next "P".
So the answers given from the user for the question number 1 are " 1 | 2 | 3 | 5 | 8 | 9 ".
I was trying to insert the values to another array with a foreach loop but i could not find any way to seperate the answers.
My desire output is.
Array
(
[P1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 8
[5] => 9
)
[P2] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 8
[5] => 9
)
)
Please tell me if you have any suggestions.Thank you very much
assuming we ve everything inside $p
$res= array();
foreach($p as $key => $value){
if(strpos($value, "P") !== false){
$currentP = $value;
$res[$currentP] = array();
}elseif($value){
$res[$currentP][] = $value;
}
}
WORKING PHP FIDDLE tested with v5 and v7
This seems to work :
<?php
$string = ',P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6';
$array = explode(',', $string);
$return = array();
$index = '';
foreach ($array as $v) {
if (trim($v) == '') // Skip empty values
continue;
if (substr($v, 0, 1) == 'P') {
$index = $v;
if (!isset($return[$index]))
$return[$index] = array();
} else {
$return[$index][] = $v;
}
}
var_dump($return);
?>
this could be an approach
<?php
$string = ',,P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6';
$sepAns = explode(',,',$string);
echo $sepAns[2];
?>
Note that I added a , at the beginning to maintain integrity of ',,'
$str = ',P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6';
$arr = explode( ',', $str );
$structuredArr = array();
foreach ( $arr AS $element ) {
if ( ( strpos( $element, 'P' ) === FALSE ) === FALSE ) {
$key = $element;
$structuredArr[ $key ] = array();
} else {
if ( isset( $key ) ) {
if ( !empty( $element ) ) {
$structuredArr[ $key ][] = $element;
}
}
}
}
This question already has answers here:
Reverse array in php
(6 answers)
Closed 8 years ago.
So this is my array, I have tried rsort and array_reverse but its sorting it numerically and that's not what I want. I just want it in the opposite order.
[15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16]
You need to add true to your array_reverse:
array array_reverse ( array $array [, bool $preserve_keys = false ] )
In other words, try something like this:
array_reverse( $yourArray, true);
This way your keys are preserved and you achieve what you need.
array_reverse should work. I just did this test:
$array = array(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16);
$rArray = array_reverse($array);
print_r($array);
print_r($rArray);
and the results were:
//for $array
Array ( [0] => 15 [1] => 14 [2] => 13 [3] => 12 [4] => 11 [5] => 10 [6] => 9 [7] => 8 [8] => 7 [9] => 6 [10] => 5 [11] => 4 [12] => 3 [13] => 2 [14] => 1 [15] => 30 [16] => 29 [17] => 28 [18] => 27 [19] => 26 [20] => 25 [21] => 24 [22] => 23 [23] => 22 [24] => 21 [25] => 20 [26] => 19 [27] => 18 [28] => 17 [29] => 16 )
//for $rArray
Array ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 [6] => 22 [7] => 23 [8] => 24 [9] => 25 [10] => 26 [11] => 27 [12] => 28 [13] => 29 [14] => 30 [15] => 1 [16] => 2 [17] => 3 [18] => 4 [19] => 5 [20] => 6 [21] => 7 [22] => 8 [23] => 9 [24] => 10 [25] => 11 [26] => 12 [27] => 13 [28] => 14 [29] => 15 )
In order to reverse an array simply put it in a stack (or at least use that logic).
FILO (First in last out) thus you have swapped the order.
A Stack Overflow question around php stacks can be found here:
PHP Stack Implementation
Are you familiar with other languages? I don't use php much.
Simply create a new object and add the end to it. Ex
new Array a
a.push(oldArray.removelast())...
The do-it-yourself-and-more-fun solution:
$oldArray = array(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16);
$length = count($oldArray);
$start= $length - 1;
$newArray=array();
for ($x = $start; $x >=0; $x--)
{
array_push($newArray,$oldArray[$x]);
}
print_r($newArray);
Prints:
Array ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 [6] => 22 [7] => 23 [8] => 24 [9] => 25 [10] => 26 [11] => 27 [12] => 28 [13] => 29 [14] => 30 [15] => 1 [16] => 2 [17] => 3 [18] => 4 [19] => 5 [20] => 6 [21] => 7 [22] => 8 [23] => 9 [24] => 10 [25] => 11 [26] => 12 [27] => 13 [28] => 14 [29] => 15 )
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I Have 3 Arrays
Array 1 :
Array ( [0] => 0 [1] => 256 [2] => 512 [3] => 768 [4] => 1024 [5] => 1280 [6] => 1536 [7] => 1792 [8] => 2048 [9] => 2304 [10] => 2560 [11] => 2816 [12] => 3072 [13] => 3328 [14] => 3584 [15] => 3840 [16] => 4096 [17] => 4352 [18] => 4608 [19] => 4864 [20] => 5120 [21] => 5376 [22] => 5632 [23] => 5888 )
Array 2 :
Array ( [0] => 0 [1] => 65536 [2] => 131072 [3] => 196608 [4] => 262144 [5] => 327680 [6] => 393216 [7] => 458752 [8] => 524288 [9] => 589824 [10] => 655360 [11] => 720896 [12] => 786432 [13] => 851968 [14] => 917504 [15] => 983040 [16] => 1048576 [17] => 1114112 [18] => 1179648 [19] => 1245184 [20] => 1310720 [21] => 1376256 [22] => 1441792 [23] => 1507328 [24] => 1572864 [25] => 1638400 [26] => 1703936 [27] => 1769472 [28] => 1835008 [29] => 1900544 [30] => 1966080 [31] => 2031616 )
Array 3 :
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 [16] => 16 [17] => 17 [18] => 18 [19] => 19 [20] => 20 [21] => 21 [22] => 22 [23] => 23 [24] => 24 [25] => 25 [26] => 26 [27] => 27 [28] => 28 [29] => 29 [30] => 30 [31] => 31 )
Given a number X which I know was calculated as a1[i] + a2[j] + a3[k] how can I calculate i, j and k?
Examples:
X = 458752 => i=0, j=7, k=0 which is 458752 = 0 + 458752 + 0
X = 131586 => i=2, j=2, k=2 which is 131586 = 256 + 131072 + 2
X = 65793 => i=1, j=1, k=1 which is 65793 = 256 + 65536 + 1
How about:
foreach(array(458752, 131586, 65793) as $x) {
$j = intval($x / 65536);
$x = $x % 65536;
$i = intval($x / 256);
$k = $x % 256;
echo "x=$x, i=$i, j=$j, k=$k\n";
}
output:
x=0, i=0, j=7, k=0
x=514, i=2, j=2, k=2
x=257, i=1, j=1, k=1
This
$value = $array1[0]+$array1[7]+$array1[0]
should work.
Array indexing begins from 0, so $array[1] is the technically the second element in an array.
First you try to post your questions clearly.
Your first array count is mismatched with other two arrays. So if the 3 arrays count must be equal, easily you can get the values. if you need to customize addition value, hard coded the array values and try addition.
Method1: All the 3 arrays count is same.
<?php
$first = Array (0 => 0, 1 => 256, 2 => 512, 3 => 768, 4 => 1024, 5 => 1280, 6 => 1536, 7 => 1792, 8 => 2048, 9 => 2304, 10 => 2560, 11 => 2816, 12 => 3072, 13 => 3328, 14 => 3584, 15 => 3840, 16 => 4096, 17 => 4352, 18 => 4608, 19 => 4864, 20 => 5120, 21 => 5376, 22 => 5632, 23 => 5888 );
$second = Array (0 => 0, 1 => 65536, 2 => 131072, 3 => 196608, 4 => 262144, 5 => 327680, 6 => 393216, 7 => 458752, 8 => 524288, 9 => 589824, 10 => 655360, 11 => 720896, 12 => 786432, 13 => 851968, 14 => 917504, 15 => 983040, 16 => 1048576, 17 => 1114112, 18 => 1179648, 19 => 1245184, 20 => 1310720, 21 => 1376256, 22 => 1441792, 23 => 1507328);
$third = Array (0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 16 => 16, 17 => 17, 18 => 18, 19 => 19, 20 => 20, 21 => 21, 22 => 22, 23 => 23);
$result = array();
foreach($first as $key => $values){
echo '<br>'.$values.'+'.$second[$key].'+'.$third[$key];
$result[] = $values+$second[$key]+$third[$key];
}
print_r($result);
?>
Method 2: (Please avoid this method)
$first_result = $first[0]+$second[7]+$third[0];
$second_result = $first[2]+$second[2]+$third[2];
$third_result = $first[1]+$second[1]+$third[1];