Gedeelte array printen zodat ik deze later kan opmaken
Het is mij na een tijdje gelukt om een api request te maken naar een api die ik wil gebruiken in een projectje. Ik heb op dit forum gelezen dat wanneer ik deze afdruk met een print_r command ik de arrays op menselijk interpreteerbare wijze kan bekijken. Dit is ook gelukt. Mijn vraag nu is, hoe kan ik uit deze array stukjes data selecteren?
Dit is de code die ik op het moment gebruik:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo '<div class="searchresult">';
$decoded = json_decode($response);
print_r($decoded);
echo '</div>';
}
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo '<div class="searchresult">';
$decoded = json_decode($response);
print_r($decoded);
echo '</div>';
}
De output die ik krijg is als volgt:
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
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
[_type] => all
[didUMean] =>
[totalCount] => 1096
[relatedSearch] => Array
(
[0] => wikipedia
[1] => <b>source wikipedia</b>
[2] => <b>wikipedia offline</b>
[3] => <b>search wikipedia</b>
[4] => <b>from wikipedia</b>
[5] => <b>wikipedia radio</b>
[6] => <b>wikipedia medical</b>
[7] => <b>search wikipedia medical</b>
[8] => <b>wikipedia trivia</b>
[9] => <b>wikipedia arcane</b>
[10] => <b>wikipedia arcane trivia</b>
[11] => <b>wikipedia arcane radio trivia</b>
)
[value] => Array
(
[0] => stdClass Object
(
[id] => 5461775494957571604
[title] => Wikipedia
[url] => http://wikipedia.org
[description] => Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia Foundation.
[body] => Read Wikipedia in your language
1000000+
[keywords] =>
[language] => en
[isSafe] => 1
[datePublished] => 0001-01-01T00:00:00
[provider] => stdClass Object
(
[name] => wikipedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[image] => stdClass Object
(
[url] =>
[height] => 0
[width] => 0
[thumbnail] =>
[thumbnailHeight] => 0
[thumbnailWidth] => 0
[base64Encoding] =>
[name] =>
[title] =>
[provider] => stdClass Object
(
[name] => wikipedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[imageWebSearchUrl] =>
[webpageUrl] => http://wikipedia.org
)
)
[1] => stdClass Object
(
[id] => 3168829338708090032
[title] => Wikipedia - Wikimedia Belgium
[url] => https://be.wikimedia.org/wiki/Wikipedia
[description] => edit
]
To improve and expand Wikipedia, often Wikipedia writing sessions are organised. These writing sessions can be organised internally for only the staff of an organisation or open for anyone interested to learn about Wikipedia and willing to edit. Wikipedia writing sessions open to the public are often referred to as
[body] => edit
]
To improve and expand Wikipedia, often Wikipedia writing sessions are organised. These writing sessions can be organised internally for only the staff of an organisation or open for anyone interested to learn about Wikipedia and willing to edit. Wikipedia writing sessions open to the public are often referred to as
[keywords] =>
[language] => en
[isSafe] => 1
[datePublished] => 0001-01-01T00:00:00
[provider] => stdClass Object
(
[name] => wikimedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[image] => stdClass Object
(
[url] =>
[height] => 0
[width] => 0
[thumbnail] =>
[thumbnailHeight] => 0
[thumbnailWidth] => 0
[base64Encoding] =>
[name] =>
[title] =>
[provider] => stdClass Object
(
[name] => wikimedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[imageWebSearchUrl] =>
[webpageUrl] => https://be.wikimedia.org/wiki/Wikipedia
)
)
[didUMean] =>
[totalCount] => 1096
[relatedSearch] => Array
(
[0] => wikipedia
[1] => <b>source wikipedia</b>
[2] => <b>wikipedia offline</b>
[3] => <b>search wikipedia</b>
[4] => <b>from wikipedia</b>
[5] => <b>wikipedia radio</b>
[6] => <b>wikipedia medical</b>
[7] => <b>search wikipedia medical</b>
[8] => <b>wikipedia trivia</b>
[9] => <b>wikipedia arcane</b>
[10] => <b>wikipedia arcane trivia</b>
[11] => <b>wikipedia arcane radio trivia</b>
)
[value] => Array
(
[0] => stdClass Object
(
[id] => 5461775494957571604
[title] => Wikipedia
[url] => http://wikipedia.org
[description] => Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia Foundation.
[body] => Read Wikipedia in your language
1000000+
[keywords] =>
[language] => en
[isSafe] => 1
[datePublished] => 0001-01-01T00:00:00
[provider] => stdClass Object
(
[name] => wikipedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[image] => stdClass Object
(
[url] =>
[height] => 0
[width] => 0
[thumbnail] =>
[thumbnailHeight] => 0
[thumbnailWidth] => 0
[base64Encoding] =>
[name] =>
[title] =>
[provider] => stdClass Object
(
[name] => wikipedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[imageWebSearchUrl] =>
[webpageUrl] => http://wikipedia.org
)
)
[1] => stdClass Object
(
[id] => 3168829338708090032
[title] => Wikipedia - Wikimedia Belgium
[url] => https://be.wikimedia.org/wiki/Wikipedia
[description] => edit
]
To improve and expand Wikipedia, often Wikipedia writing sessions are organised. These writing sessions can be organised internally for only the staff of an organisation or open for anyone interested to learn about Wikipedia and willing to edit. Wikipedia writing sessions open to the public are often referred to as
[body] => edit
]
To improve and expand Wikipedia, often Wikipedia writing sessions are organised. These writing sessions can be organised internally for only the staff of an organisation or open for anyone interested to learn about Wikipedia and willing to edit. Wikipedia writing sessions open to the public are often referred to as
[keywords] =>
[language] => en
[isSafe] => 1
[datePublished] => 0001-01-01T00:00:00
[provider] => stdClass Object
(
[name] => wikimedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[image] => stdClass Object
(
[url] =>
[height] => 0
[width] => 0
[thumbnail] =>
[thumbnailHeight] => 0
[thumbnailWidth] => 0
[base64Encoding] =>
[name] =>
[title] =>
[provider] => stdClass Object
(
[name] => wikimedia
[favIcon] =>
[favIconBase64Encoding] =>
)
[imageWebSearchUrl] =>
[webpageUrl] => https://be.wikimedia.org/wiki/Wikipedia
)
)
Dit gaat dan door tot array [9]. Dus 10 arrays in totaal.
Ik wil hier dan de totalCount en relatedSearch van printen, en voor iedere volgende array de title, url en description. Ik geloof dat ik voor het laatste een (foreach?) loop moet maken die alle arrays stuk voor stuk af gaat. Ik kan hier alleen niets over vinden als ik dit opzoek (waarschijnlijk zoek ik verkeerd).
Ik hoop dat iemand mij de juiste richting in kan sturen!
Met vriendelijke groet,
Joris
En verder voor 'relatedSearch' inderdaad een foreach. Zie ook https://www.w3schools.com/php/php_looping_foreach.asp
Horen de keys van 'relatedSearch' en 'value' bij elkaar?
Gewijzigd op 18/02/2021 13:22:37 door - Ariën -
Ik geloof dat de keys van relatedSearch en value niet bij elkaar horen, maar volgensmij vallen de andere arrays (de resultaten zelf) wel onder value horen.
Toevoeging op 18/02/2021 14:20:36:
Mijn foreach loop lijkt niet helemaal te werken. Ik ben vrij zeker dat ik iets verkeerd doe maar zie niet helemaal in wat ik mis doe. Het printen van de totalCount lukt wel, maar ik weet niet hoe ik de titel/url/description uit de arrays moet halen.
Dit is wat ik nu heb:
Probeer dit eens:
Of als het root-object een stdClass is:
Goed lezen is ook voor mij soms moeilijk, het is een stdClass.
Gewijzigd op 18/02/2021 14:28:00 door Thom nvt
geweldig! Heel erg bedankt! Zonder jullie hulp was ik hier waarschijnlijk ngo wel een maandje zoet mee haha.