array index zoeken in een object array
Ik wil in de volgende array een de array index vinden van een waarde die de string 16 bevat op de positie d0.
De array ziet er als volgt uit:
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
print_r($rows);
// RESULTAAT print_r($rows)
Array
(
[0] => stdClass Object
(
[id] => 1
[MOER_id] => 1
[Type] => FK 1605
[dd1] => 15.6
[d0] => 16
[P] => 5
[d3] => 12.7
[i] => 3
[D1] => 28
[D4] => 38
[D5] => 5.5
[D6] => 48
[L] => 48.5
[L0] => 33.0
[L7] => 10
[L8] => 40
[Ca] => 9.5
[Coa] => 10.9
)
[1] => stdClass Object
(
[id] => 2
[MOER_id] => 1
[Type] => FK 2005
[dd1] => 19.6
[d0] => 20
[P] => 5
[d3] => 16.7
[i] => 3
[D1] => 36
[D4] => 47
[D5] => 6.6
[D6] => 58
[L] => 48.5
[L0] => 33.0
[L7] => 10
[L8] => 44
[Ca] => 11.5
[Coa] => 15.5
)
[2] => stdClass Object
(
[id] => 3
[MOER_id] => 1
[Type] => FK 2505
[dd1] => 24.6
[d0] => 25
[P] => 5
[d3] => 21.7
[i] => 3
[D1] => 40
[D4] => 51
[D5] => 6.6
[D6] => 62
[L] => 49.0
[L0] => 33.0
[L7] => 10
[L8] => 48
[Ca] => 13.1
[Coa] => 20.2
)
[3] => stdClass Object
(
[id] => 4
[MOER_id] => 1
[Type] => FK 3205
[dd1] => 31.6
[d0] => 32
[P] => 5
[d3] => 28.7
[i] => 4
[D1] => 50
[D4] => 65
[D5] => 9.0
[D6] => 80
[L] => 57.0
[L0] => 39.0
[L7] => 12
[L8] => 62
[Ca] => 19.3
[Coa] => 36.3
)
[4] => stdClass Object
(
[id] => 5
[MOER_id] => 1
[Type] => FK 3210
[dd1] => 31.6
[d0] => 32
[P] => 10
[d3] => 27.1
[i] => 3
[D1] => 50
[D4] => 65
[D5] => 9.0
[D6] => 80
[L] => 73.0
[L0] => 55.0
[L7] => 12
[L8] => 62
[Ca] => 26.4
[Coa] => 39.0
)
[5] => stdClass Object
(
[id] => 6
[MOER_id] => 1
[Type] => FK 4005
[dd1] => 39.6
[d0] => 40
[P] => 5
[d3] => 36.7
[i] => 5
[D1] => 63
[D4] => 78
[D5] => 9.0
[D6] => 93
[L] => 66.0
[L0] => 45.0
[L7] => 14
[L8] => 70
[Ca] => 26.3
[Coa] => 59.2
)
[6] => stdClass Object
(
[id] => 7
[MOER_id] => 1
[Type] => FK 4010
[dd1] => 39.6
[d0] => 40
[P] => 10
[d3] => 34.0
[i] => 4
[D1] => 63
[D4] => 78
[D5] => 9.0
[D6] => 93
[L] => 88.5
[L0] => 67.5
[L7] => 14
[L8] => 70
[Ca] => 64.9
[Coa] => 109.0
)
[7] => stdClass Object
(
[id] => 8
[MOER_id] => 1
[Type] => FK 5010
[dd1] => 49.5
[d0] => 50
[P] => 10
[d3] => 43.0
[i] => 4
[D1] => 75
[D4] => 93
[D5] => 11.0
[D6] => 110
[L] => 92.0
[L0] => 69.0
[L7] => 16
[L8] => 85
[Ca] => 66.4
[Coa] => 134.3
)
[8] => stdClass Object
(
[id] => 9
[MOER_id] => 1
[Type] => FK 6310
[dd1] => 62.5
[d0] => 63
[P] => 10
[d3] => 56.9
[i] => 5
[D1] => 90
[D4] => 108
[D5] => 11.0
[D6] => 125
[L] => 103.5
[L0] => 78.5
[L7] => 18
[L8] => 95
[Ca] => 93.8
[Coa] => 229.7
)
[9] => stdClass Object
(
[id] => 10
[MOER_id] => 1
[Type] => FK 8010
[dd1] => 79.5
[d0] => 80
[P] => 10
[d3] => 73.9
[i] => 6
[D1] => 105
[D4] => 125
[D5] => 13.5
[D6] => 145
[L] => 121.0
[L0] => 92.0
[L7] => 20
[L8] => 110
[Ca] => 121.9
[Coa] => 374.9
)
)
?>
print_r($rows);
// RESULTAAT print_r($rows)
Array
(
[0] => stdClass Object
(
[id] => 1
[MOER_id] => 1
[Type] => FK 1605
[dd1] => 15.6
[d0] => 16
[P] => 5
[d3] => 12.7
[i] => 3
[D1] => 28
[D4] => 38
[D5] => 5.5
[D6] => 48
[L] => 48.5
[L0] => 33.0
[L7] => 10
[L8] => 40
[Ca] => 9.5
[Coa] => 10.9
)
[1] => stdClass Object
(
[id] => 2
[MOER_id] => 1
[Type] => FK 2005
[dd1] => 19.6
[d0] => 20
[P] => 5
[d3] => 16.7
[i] => 3
[D1] => 36
[D4] => 47
[D5] => 6.6
[D6] => 58
[L] => 48.5
[L0] => 33.0
[L7] => 10
[L8] => 44
[Ca] => 11.5
[Coa] => 15.5
)
[2] => stdClass Object
(
[id] => 3
[MOER_id] => 1
[Type] => FK 2505
[dd1] => 24.6
[d0] => 25
[P] => 5
[d3] => 21.7
[i] => 3
[D1] => 40
[D4] => 51
[D5] => 6.6
[D6] => 62
[L] => 49.0
[L0] => 33.0
[L7] => 10
[L8] => 48
[Ca] => 13.1
[Coa] => 20.2
)
[3] => stdClass Object
(
[id] => 4
[MOER_id] => 1
[Type] => FK 3205
[dd1] => 31.6
[d0] => 32
[P] => 5
[d3] => 28.7
[i] => 4
[D1] => 50
[D4] => 65
[D5] => 9.0
[D6] => 80
[L] => 57.0
[L0] => 39.0
[L7] => 12
[L8] => 62
[Ca] => 19.3
[Coa] => 36.3
)
[4] => stdClass Object
(
[id] => 5
[MOER_id] => 1
[Type] => FK 3210
[dd1] => 31.6
[d0] => 32
[P] => 10
[d3] => 27.1
[i] => 3
[D1] => 50
[D4] => 65
[D5] => 9.0
[D6] => 80
[L] => 73.0
[L0] => 55.0
[L7] => 12
[L8] => 62
[Ca] => 26.4
[Coa] => 39.0
)
[5] => stdClass Object
(
[id] => 6
[MOER_id] => 1
[Type] => FK 4005
[dd1] => 39.6
[d0] => 40
[P] => 5
[d3] => 36.7
[i] => 5
[D1] => 63
[D4] => 78
[D5] => 9.0
[D6] => 93
[L] => 66.0
[L0] => 45.0
[L7] => 14
[L8] => 70
[Ca] => 26.3
[Coa] => 59.2
)
[6] => stdClass Object
(
[id] => 7
[MOER_id] => 1
[Type] => FK 4010
[dd1] => 39.6
[d0] => 40
[P] => 10
[d3] => 34.0
[i] => 4
[D1] => 63
[D4] => 78
[D5] => 9.0
[D6] => 93
[L] => 88.5
[L0] => 67.5
[L7] => 14
[L8] => 70
[Ca] => 64.9
[Coa] => 109.0
)
[7] => stdClass Object
(
[id] => 8
[MOER_id] => 1
[Type] => FK 5010
[dd1] => 49.5
[d0] => 50
[P] => 10
[d3] => 43.0
[i] => 4
[D1] => 75
[D4] => 93
[D5] => 11.0
[D6] => 110
[L] => 92.0
[L0] => 69.0
[L7] => 16
[L8] => 85
[Ca] => 66.4
[Coa] => 134.3
)
[8] => stdClass Object
(
[id] => 9
[MOER_id] => 1
[Type] => FK 6310
[dd1] => 62.5
[d0] => 63
[P] => 10
[d3] => 56.9
[i] => 5
[D1] => 90
[D4] => 108
[D5] => 11.0
[D6] => 125
[L] => 103.5
[L0] => 78.5
[L7] => 18
[L8] => 95
[Ca] => 93.8
[Coa] => 229.7
)
[9] => stdClass Object
(
[id] => 10
[MOER_id] => 1
[Type] => FK 8010
[dd1] => 79.5
[d0] => 80
[P] => 10
[d3] => 73.9
[i] => 6
[D1] => 105
[D4] => 125
[D5] => 13.5
[D6] => 145
[L] => 121.0
[L0] => 92.0
[L7] => 20
[L8] => 110
[Ca] => 121.9
[Coa] => 374.9
)
)
?>
Hoe doe ik dat?
Gewijzigd op 25/04/2012 09:23:22 door Hans Jonker
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
for ($i = 1; ; $i++)
{
$pos = array_search(16, $array[$i]['d0']);
if($pos !=== false)
{
echo 'Pos d0 is 16!';
}
}
?>
for ($i = 1; ; $i++)
{
$pos = array_search(16, $array[$i]['d0']);
if($pos !=== false)
{
echo 'Pos d0 is 16!';
}
}
?>
In bovenstaand voorbeeld van Eddy krijg ik een true or false terug en dat is niet wat ik nodig heb.
Code (php)
Gewijzigd op 24/04/2012 22:48:22 door Christiaan Scheermeijer
Als ik jouw code gebruik krijg ik een fout melding;
Fatal error: Cannot use object of type stdClass as array in .....
Toevoeging op 25/04/2012 09:16:11:
Ik heb een oplossing gevonden wat voor mij werkt, ik weet alleen niet of het goed programeren is.
Ik heb dus een array $rows die gevuld is met de code zoals helemaal boven aan getoond.
Het is dus een array gevuld met stdClass Objects.
Op mijn site vraag ik de gebruiker welke type hij wil zien d.m.v. een form en een submit button
de waarde die ik daar uit krijg zet ik om in een variable met de volgende code
Code (php)
Om de index te vinden van de array $rows aan de hand ven de gekozen type gebruik ik de volgende code;
Code (php)
Toevoeging op 25/04/2012 09:25:19:
Christiaan Scheermeijer op 24/04/2012 22:47:33:
Gewijzigd op 25/04/2012 09:20:24 door Hans Jonker
Aangepast:
Je kan ook array_key() gebruiken?