Hulp bij array
Dus bij onderstaande array zou ik bijv, dit in de database willen zien:
1. deviceid -- posx -- posy ---knob_value0
1. deviceid -- posx -- posy ---knob_value1
1. deviceid -- posx -- posy ---knob_value2
1. deviceid -- posx -- posy ---knob_value3
2. deviceid -- posx -- posy ---knob_value0
2. deviceid -- posx -- posy ---knob_value1
2. deviceid -- posx -- posy ---knob_value2
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
array(4) {
["deviceid"]=>
array(2) {
[1]=>
string(38) "49429e6b-f64d-4803-a2e5-f65ed03bab0e_1"
[2]=>
string(38) "01078a35-3b20-4d0f-a16e-77c986faa7c1_2"
}
["posx"]=>
array(2) {
[1]=>
string(2) "17"
[2]=>
string(2) "85"
}
["posy"]=>
array(2) {
[1]=>
string(2) "39"
[2]=>
string(7) "88.3125"
}
["knob_value"]=>
array(2) {
[1]=>
array(4) {
[0]=>
string(41) "4a31f74b-81d4-4b16-8aab-502abd44d1bf_1|44"
[1]=>
string(41) "7987db78-8bb0-419f-804a-f444db03fa57_1|72"
[2]=>
string(41) "8a643e5b-b54d-488b-a356-c80cd160bd1c_1|81"
[3]=>
string(41) "94023cd0-4d12-448c-8a15-363bb2ad0d39_1|72"
}
[2]=>
array(3) {
[0]=>
string(42) "51db1019-2809-4826-aad5-bbc6408af92e_2|124"
[1]=>
string(42) "5fd45c46-3645-4061-8c35-083023169c3a_2|121"
[2]=>
string(42) "9fc702ef-c86e-4084-bb74-8af161f8880b_2|100"
}
}
}
["deviceid"]=>
array(2) {
[1]=>
string(38) "49429e6b-f64d-4803-a2e5-f65ed03bab0e_1"
[2]=>
string(38) "01078a35-3b20-4d0f-a16e-77c986faa7c1_2"
}
["posx"]=>
array(2) {
[1]=>
string(2) "17"
[2]=>
string(2) "85"
}
["posy"]=>
array(2) {
[1]=>
string(2) "39"
[2]=>
string(7) "88.3125"
}
["knob_value"]=>
array(2) {
[1]=>
array(4) {
[0]=>
string(41) "4a31f74b-81d4-4b16-8aab-502abd44d1bf_1|44"
[1]=>
string(41) "7987db78-8bb0-419f-804a-f444db03fa57_1|72"
[2]=>
string(41) "8a643e5b-b54d-488b-a356-c80cd160bd1c_1|81"
[3]=>
string(41) "94023cd0-4d12-448c-8a15-363bb2ad0d39_1|72"
}
[2]=>
array(3) {
[0]=>
string(42) "51db1019-2809-4826-aad5-bbc6408af92e_2|124"
[1]=>
string(42) "5fd45c46-3645-4061-8c35-083023169c3a_2|121"
[2]=>
string(42) "9fc702ef-c86e-4084-bb74-8af161f8880b_2|100"
}
}
}
Ik kom er daarentegen niet uit hoe ik de waarden op een goede manier uit de array kan halen zodat ik ze in een database kan inlezen. Kan iemand me hierbij op weg helpen?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$a = [
'deviceid' => [
1 => '49429e6b-f64d-4803-a2e5-f65ed03bab0e_1',
'01078a35-3b20-4d0f-a16e-77c986faa7c1_2'],
'posx' => [1 => '17', '85'],
'posy' => [2 => '39', '88.3125'],
'knob_value' => [
1 => ['4a31f74b-81d4-4b16-8aab-502abd44d1bf_1|44',
'7987db78-8bb0-419f-804a-f444db03fa57_1|72',
'8a643e5b-b54d-488b-a356-c80cd160bd1c_1|81',
'94023cd0-4d12-448c-8a15-363bb2ad0d39_1|72'],
['51db1019-2809-4826-aad5-bbc6408af92e_2|124',
'5fd45c46-3645-4061-8c35-083023169c3a_2|121',
'9fc702ef-c86e-4084-bb74-8af161f8880b_2|100']
]];
?>
var_dump($a);
$a = [
'deviceid' => [
1 => '49429e6b-f64d-4803-a2e5-f65ed03bab0e_1',
'01078a35-3b20-4d0f-a16e-77c986faa7c1_2'],
'posx' => [1 => '17', '85'],
'posy' => [2 => '39', '88.3125'],
'knob_value' => [
1 => ['4a31f74b-81d4-4b16-8aab-502abd44d1bf_1|44',
'7987db78-8bb0-419f-804a-f444db03fa57_1|72',
'8a643e5b-b54d-488b-a356-c80cd160bd1c_1|81',
'94023cd0-4d12-448c-8a15-363bb2ad0d39_1|72'],
['51db1019-2809-4826-aad5-bbc6408af92e_2|124',
'5fd45c46-3645-4061-8c35-083023169c3a_2|121',
'9fc702ef-c86e-4084-bb74-8af161f8880b_2|100']
]];
?>
var_dump($a);
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
$count = count($data['deviceid']); //aantal waarden
for($i = 1; $i <= $count; $i++){ //alle waarden langs lopen (base 1)
$deviceid = $data['deviceid'][$i];
$posx = $data['posx'][$i];
$posy = $data['posy'][$i];
foreach($data['knob_value'][$i] as $knob_value){
$sql = "
insert into tabel (deviceid,posx,posy,knob_value)
values ('$deviceid',$posx,$posy,'$knob_value')";
//$sql uitvoeren
}
}
?>
$count = count($data['deviceid']); //aantal waarden
for($i = 1; $i <= $count; $i++){ //alle waarden langs lopen (base 1)
$deviceid = $data['deviceid'][$i];
$posx = $data['posx'][$i];
$posy = $data['posy'][$i];
foreach($data['knob_value'][$i] as $knob_value){
$sql = "
insert into tabel (deviceid,posx,posy,knob_value)
values ('$deviceid',$posx,$posy,'$knob_value')";
//$sql uitvoeren
}
}
?>
Gewijzigd op 15/09/2021 23:16:39 door Rob Doemaarwat
Ik heb het nog niet geprobeerd maar dat ziet er al logisch uit. Ik hoop er morgenavond even verder naar te kijken. Bedankt alvast voor de hulp!
Dit werkt prima! Dank!
walgreenslistens
Thanks for the information, I will try to figure it out for more...Gewijzigd op 20/10/2021 05:51:54 door Berry Brink